Thursday, March 26, 2015

Converter for changing Textblock Foreground

Hello Everybody !!


I need to change dynamically the foreground of a textblock.


I developed a converter :



public class BooleanToColorConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null)
{
bool val = (bool)value;
if (parameter != null && string.Compare(parameter as string, "calendar", StringComparison.CurrentCultureIgnoreCase) == 0)
{
return val == true ? new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)) : new SolidColorBrush(Color.FromArgb(0, 140,140,140));
}

}
return false;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

And this is my XAML Code :



<TextBlock TextWrapping="Wrap" Grid.Column="1"
Foreground="{Binding IsWriteInRed, Converter={StaticResource BooleanToColorConverter}, ConverterParameter=calendar}"
SelectionHighlightColor="{StaticResource WhiteSolidColorBrush}" Margin="5,0" VerticalAlignment="Center">
<Run Text="{Binding Company.Name}"/>
<Run Text=":"/>
<Run Text="{Binding Subject}"/>
</TextBlock>

It doesn't work, I don't know why...any ideas please??


Thanks


No comments:

Post a Comment