I bind some property to mine TextBlock:
<TextBlock
Text="{Binding Status}"
Foreground="{Binding RealTimeStatus,Converter={my:RealTimeStatusToColorConverter}}"
/>
Status- plain text, and RealTimeStatus- enum. For each value, enumI change the color TextBlock Foreground.
Sometimes my message Statuscontains numbers. This message gets the appropriate color according to the value enum, but I wonder if I can change the colors of the numbers inside this message so that the numbers will be different from the rest.
Change
Xaml
<TextBlock my:TextBlockExt.XAMLText="{Binding Status, Converter={my:RealTimeStatusToColorConverter}}"/>
Converter
public class RealTimeStatusToColorConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is RealTimeStatus && targetType == typeof(Brush))
{
switch ((RealTimeStatus)value)
{
case RealTimeStatus.Cancel:
case RealTimeStatus.Stopped:
return Brushes.Red;
case RealTimeStatus.Done:
return Brushes.White;
case RealTimeStatus.PacketDelay:
return Brushes.Salmon;
default:
break;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public RealTimeStatusToColorConverter()
{
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
source
share