What does your IValueConverter doesn't work refer to? Did the data binding fail
or the converter fail
?
Data binding fail: Please add below class and this.DataContext = new MyModel();
in xaml.cs:
public class MyModel : INotifyPropertyChanged
{
public MyModel()
{
}
private int age;
private string name;
public event PropertyChangedEventHandler PropertyChanged;
public int Age
{
get { return age; }
set
{
age = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("age"));
}
}
}
public string Name
{
get { return name; }
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("name"));
}
}
}
}
Converter fail: I test your converters, but the second converter in ageTextBox
doesn't work. I add triggers in your xam as below:
<Window.Resources>
<local:AgeToForegroundConverter x:Key="ageConverter"/>
<local:Base16Converter x:Key="base16Converter"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row ="0" Grid.Column ="0" Margin ="5" VerticalAlignment =" Center"> Name:</TextBlock>
<TextBox Name ="nameTextBox" Text ="{Binding Path = Name}" Grid.Row ="0" Grid.Column ="1" Margin ="5" />
<TextBlock Grid.Row="1" Grid.Column="0" Margin="5" VerticalAlignment="Center">Age:</TextBlock>
<TextBox Name="ageTextBox" Foreground="{Binding Path=Age, Converter={ StaticResource ageConverter}}" Text="{Binding Path=Age, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" Margin="5">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Visibility" Value="Visible"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ageTextBox,Path=Visibility}" Value="Hidden">
<Setter Property="Text" Value="AAA" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ageTextBox1,Path=Visibility}" Value="Hidden">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Background" Value="LightBlue"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ageTextBox1,Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Name="birthdayButton" Foreground="{Binding Path = Foreground, ElementName=ageTextBox}" Content="Birthday" Grid.Row="2" Grid.Column="1" Margin="5"></Button>
<TextBox Name="ageTextBox1" Foreground="{Binding Path=Age, Converter={ StaticResource ageConverter}}" Text="{Binding Path=Age, Converter={StaticResource base16Converter}}" Grid.Row="1" Grid.Column="1" Margin="5">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Visibility" Value="Hidden"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ageTextBox,Path=Foreground}" Value="Red">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Background" Value="Yellow"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}" Value="">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
Please point out if I misunderstand your question.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.