Wpf Checkbox content UnderscoreConverter in ResourceDictionary?

harry777 61 Reputation points
2020-09-10T12:02:05.337+00:00

Hi,

How to use an UnderscoreConverter for CheckBox CONTENTS in ResourceDictionary?
Below doesn't work.

<Style TargetType="CheckBox">
    <Setter Property="Content">
        <Setter.Value>
            <Binding>
                <Binding.Converter>
                    <converters:UnderscoreConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

Cheers,
Harry

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-09-11T02:39:03.76+00:00

    I will show you my demo to use Converter in ResourceDictionary.
    Step 1: Create a model named MyModel which used for binding data to CheckBox.

     public class MyModel: INotifyPropertyChanged  
        {  
            public string Name { get; set; }  
            public bool StatusForCheckBox { get; set; }  
      
            public string myBrushes;  
            public string MyBrushes  
            {  
                get { return myBrushes; }  
                set  
                {  
                    myBrushes = value;  
                    NotifyPropertyChanged("MyBrushes");  
                }  
            }  
      
            public event PropertyChangedEventHandler PropertyChanged;  
            public void NotifyPropertyChanged(string propertyName)  
            {  
                if (PropertyChanged != null)  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
                }  
            }  
        }  
    

    Step 2:Create a list for CheckBox in the MainWindow.xaml

     <Grid>  
            <ListBox x:Name="myList" Width="400" Height="400" HorizontalAlignment="Right">  
                <ListBox.ItemTemplate>  
                    <DataTemplate >  
                        <CheckBox Content="{Binding Name,Mode=TwoWay}" IsChecked="{Binding StatusForCheckBox,Mode=TwoWay}" Background="{Binding MyBrushes,Mode=TwoWay}"/>  
                    </DataTemplate>  
                </ListBox.ItemTemplate>  
            </ListBox>  
        </Grid>  
    

    Step 3: Binding the data for the list in MainWindow.xaml.cs:

     public MainWindow()  
            {  
                InitializeComponent();  
                ObservableCollection<MyModel> lt = new ObservableCollection<MyModel>()  
                {  
                    new MyModel{Name="Test1",StatusForCheckBox=false },  
                    new MyModel{Name="Test2",StatusForCheckBox=true },  
                    new MyModel{Name="Test3",StatusForCheckBox=false}  
                };  
                myList.ItemsSource = lt;  
            }  
    

    Step 4: Create a Converter named MyConverter, and its code is:

     public class MyConverter : IValueConverter  
        {  
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                MyModel model = value as MyModel;  
                bool bValue = model.StatusForCheckBox;  
                if (bValue != true)  
                {  
                    model.MyBrushes = "Red";  
                    return model;  
                }  
                else  
                {  
                    model.MyBrushes = "Yellow";  
                    return model;  
                }  
            }  
      
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                throw new NotImplementedException();  
            }  
        }  
    

    Step 5:Add the style in ResourceDictionary:

    <Application.Resources>  
            <Style TargetType="CheckBox">  
                <Setter Property="Content">  
                    <Setter.Value>  
                        <Binding>  
                            <Binding.Converter>  
                                <local:MyConverter></local:MyConverter>  
                            </Binding.Converter>  
                        </Binding>  
                    </Setter.Value>  
                </Setter>  
            </Style>  
        </Application.Resources>  
    

    Then you can get the result picture:
    24015-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. harry777 61 Reputation points
    2020-09-11T08:15:54.227+00:00

    Hi Daisy ,

    Thanks for your reply. I tried to implement UnderscoreConverter in your above project but it's not working for me.

    public class UnderscoreConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                MyModel model = value as MyModel;
                if (model.Name is string s)
                    return s.Replace("_", "__");
                return null;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
        }
    
    <Application.Resources>
            <Style TargetType="CheckBox">
                <Setter Property="Content">
                    <Setter.Value>
                        <Binding>
                            <Binding.Converter>
                                <local:UnderscoreConverter/>
                            </Binding.Converter>
                        </Binding>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>
    

    I must be doing something fundamentally wrong. In your example value property is of type 'MyModel' is there a way we make UnderscoreConverter that targets the checkbox content's text property directly because I have different types defining my checkbox in views.

    I understand one way out is to use TextBlock to represent CB contents but I cannot use TextBlock in my project.

    Kind regards,
    Harry


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.