I would use the Boolean to Style converter.
public class BoolToStyleConverter : IValueConverter { public Style TrueStyle { get; set; } public Style FalseStyle { get; set; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((bool)value) ? TrueStyle : FalseStyle; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Then, in the resources section, you would set 2 open style properties.
<localHelpers:BoolToStyleConverter x:Key="boolToHistoryTextBlockStyleConverter"> <localHelpers:BoolToStyleConverter.TrueStyle> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Red"></Setter> </Style> </localHelpers:BoolToStyleConverter.TrueStyle> <localHelpers:BoolToStyleConverter.FalseStyle> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Black"></Setter> </Style> </localHelpers:BoolToStyleConverter.FalseStyle> </localHelpers:BoolToStyleConverter>
This example sets the foreground color, but you can set any style. To bind this, you must install the converter, in this case, if IsCommentChange is True, the text will be red, if it is incorrect, then it is black.
<TextBlock Name="tbComment" Text="{Binding Path=Comment,Mode=OneTime}" TextWrapping="Wrap" Style="{Binding Path=IsCommentChanged, Converter={StaticResource boolToHistoryTextBlockStyleConverter}}" />
source share