简体   繁体   中英

Change font color by WPF Converter

Into my xaml I have label:

<Label Content="{Binding Path=HSValidation, Converter={StaticResource HSFontConverter}}" />

Into converter I want to change label font to "RED":

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{           
  if (value != null)
   {

       return value; //should be value with changed color       
   }          
  return value;  
}

Can I use just c# code?

OR do I need name property of my label?

Converters only convert the source value to something else, so you would only be able to convert the "Content" property to something else with your binding.

So you should have your content be bound to something as you have and then have the "Foreground" property be bound to the color.

<Label 
    Content="{Binding Path=HSValidation}" 
    Foreground="{Binding Path=HSValidation, Converter={StaticResource HSFontConverter}}" />

You should return Brushes.Red for instance in your converter code, and a Brush on all branches in the converter, not the bound value:

public object Convert(
    object value, Type targetType, object parameter, CultureInfo culture)
{           
    if (value != null)
    {
        return Brushes.Red;
    }          

    return Brushes.Black;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM