locked
Checkbox: changing the text style when the Checkbox is checked RRS feed

  • Question

  • I'd like to either make the text bold or a different color when a checkbox is selected. I know there is a visual state manager for checkstates, but don't see any way to change the color or weight of the content presenters font. Does anyone have an example of a style that will accomplish this?
    Wednesday, December 2, 2009 4:31 PM

Answers

  • You can do this using a converter to change the "Foreground" property based on the value of the "IsChecked" property....

     

    <UserControl
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:SilverlightApplication95"
     x:Class="SilverlightApplication95.MainPage"
    >
    
      <UserControl.Resources>
        <local:MyConverter x:Key="MyConverter"/>
      </UserControl.Resources>
    
      <Grid Background="White">
    
        <CheckBox x:Name="checkBox" Content="This is my checkbox content..." Foreground="{Binding IsChecked, ElementName=checkBox, Mode=OneWay, Converter={StaticResource MyConverter}}" />
    
      </Grid>
      
    </UserControl>
    
     
    public class MyConverter : System.Windows.Data.IValueConverter
    {
      
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        bool IsChecked = (bool)value;
        return (IsChecked ? new SolidColorBrush(Colors.Green) :  new SolidColorBrush(Colors.Red) );
      }
    
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }
            
    }
     
    Wednesday, December 2, 2009 5:16 PM

All replies

  • You can do this using a converter to change the "Foreground" property based on the value of the "IsChecked" property....

     

    <UserControl
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:SilverlightApplication95"
     x:Class="SilverlightApplication95.MainPage"
    >
    
      <UserControl.Resources>
        <local:MyConverter x:Key="MyConverter"/>
      </UserControl.Resources>
    
      <Grid Background="White">
    
        <CheckBox x:Name="checkBox" Content="This is my checkbox content..." Foreground="{Binding IsChecked, ElementName=checkBox, Mode=OneWay, Converter={StaticResource MyConverter}}" />
    
      </Grid>
      
    </UserControl>
    
     
    public class MyConverter : System.Windows.Data.IValueConverter
    {
      
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        bool IsChecked = (bool)value;
        return (IsChecked ? new SolidColorBrush(Colors.Green) :  new SolidColorBrush(Colors.Red) );
      }
    
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }
            
    }
     
    Wednesday, December 2, 2009 5:16 PM
  • hello!

    Thank You for solution!

    I advise to use to current control, without "Element Name":

     <CheckBox   Foreground="{Binding Mode=OneWay, Converter={StaticResource IsCheckedToForegroundConventer}, Path=IsChecked, RelativeSource={RelativeSource Self}}" />            
    Monday, March 19, 2012 5:10 AM