Right now, I’m learning WPF. Can we change another WPF object’s property when an WPF object’s property is changed?
Below is simplified scenario.
I have a Window with a TextBox named m_Text and a ToggleButton named m_Button. I want to change the m_Text.Background property if m_Button is pressed, that is m_Button.IsChecked = true. I think it’s possible using a Trigger but I don’t know how to do it.
P.S. If possible, I want to do it only in XAML.
WPF makes this really easy – you can databind the TextBox’s Background property directly to the IsChecked property on the ToggleButton. Of course, you will need to convert the IsChecked (boolean) to be a Brush, but WPF allows you to specify a Converter object right in the binding…
In code, you create an object that implements
IValueConverter, and implement the Convert method, likethen in xaml you need to add the namespace containing this class, declare an instance of the converter as a resource within the window, then use it in the Binding… it should look something like this:
UPDATE: As per Ivan’s excellent suggestion – have updated to show how you can pass parameters through to the Converter from the XAML…