Elad770
Member
- Joined
- Oct 4, 2021
- Messages
- 20
- Programming Experience
- 1-3
Hello
I am trying to bind between a property of a string array and some Label defined in xaml.
I'm just like that:
the binding not working with string[], all the rest of the properties work great.
By the way I do not inherit INotifyPropertyChanged because I use with a package from Nuget called Fody that saves the writing of the PropertyChangedEventHandler event and writing on all properties
I am trying to bind between a property of a string array and some Label defined in xaml.
I'm just like that:
C#:
//ViewModel
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
public string Id { get; set; }
public bool IsVisable { get; set; }
public string[] Massages { get; set; }
public User()
{
Massages = new string[3];
}
}
//code behind
public MainWindow()
{
InitializeComponent();
DataContext = new User()
{
UserName = "blbla",
Password = "AAA000YYSS",
Id = "123123098S",
Massages = new string[]{"UserName invalid","Password invalid","Id invalid"}
};
}
//xaml
<Window>
<Grid Name="grid" HorizontalAlignment="Center" Margin="0,50,0,0">
<Grid.RowDefinitions>
<!--0-->
<RowDefinition Height="110"/>
<!--1-->
<RowDefinition Height="40"/>
<!--2-->
<RowDefinition Height="30"/>
<!--3-->
<RowDefinition Height="30"/>
<!--4-->
<RowDefinition Height="auto" />
<!--5-->
<RowDefinition Height="30"/>
<!--6-->
<RowDefinition Height="30"/>
<!--7-->
<RowDefinition Height="auto" />
<!--8-->
<RowDefinition Height="30"/>
<!--9-->
<RowDefinition Height="30"/>
<!--10-->
<RowDefinition Height="auto" />
<!--11-->
<RowDefinition Height="8"/>
<RowDefinition Height="41"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Name="titleLogin" Grid.Row="1" Text="Login" Foreground="#FF7A7171" FontSize="23" FontWeight="Bold" HorizontalAlignment="Center"/>
<TextBlock Name="lUserName" FontWeight="Bold" Grid.Column="1" Grid.Row="2" Text="User Name" Foreground="#FF7A7171" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
<TextBox x:Name="txtUsername" Height="30" Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource }" Grid.Row="3" Style="{DynamicResource textboxPasswordboxStyles}" Width="290" />
<!--Binding Massages[0] not working-->
<Label Name="massageErrorUserName" Grid.Row="4" Grid.Column="1" FontSize="14" Foreground="Red" Content="{Binding Massages[0], Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Password" Grid.Row="5" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontWeight="Bold" Foreground="#FF7A7171" />
<PasswordBox x:Name="txtPassword" PasswordChanged="txtPassword_PasswordChanged" Style="{DynamicResource textboxPasswordboxStyles}" Height="{Binding ElementName=txtUsername, Path=Height}" Grid.Column="1" Width="{Binding ElementName=txtUsername, Path=Width}" Grid.Row="6" />
<!--Binding Massages[1] not working-->
<Label Name="massageErrorPassword" Grid.Row="7" Foreground="Red" Grid.Column="1" FontSize="14" Content="{Binding user.Massages[1], Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="ID" FontWeight="Bold" Grid.Row="8" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FF7A7171" />
<TextBox x:Name="txtId" Text="{Binding Id, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource }" Height="{Binding ElementName=txtUsername, Path=Height}" Style="{DynamicResource textboxPasswordboxStyles}" FontSize="21" Width="{Binding ElementName=txtUsername, Path=Width}" Grid.Row="9" />
<!--Binding Massages[2] not working-->
<Label Name="massageErrorId" Grid.Row="10" Grid.Column="1" FontSize="14" Foreground="Red" Content="{Binding Massages[2], Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
<Label Name="massageError" Visibility="{Binding IsVisable,
Converter={StaticResource BooleanToVisibilityConverter}}" Content="The user is not registered" Grid.Row="10" Grid.Column="1" FontSize="14" Foreground="Red" />
<Button x:Name="btn" Content="Login" FontSize="18" Grid.Row="12" Style="{StaticResource btn-primary}" Click="Btn_Click" />
</Grid>
</Window>
By the way I do not inherit INotifyPropertyChanged because I use with a package from Nuget called Fody that saves the writing of the PropertyChangedEventHandler event and writing on all properties
Last edited: