tags:

views:

44

answers:

1

Hi, right now, I'm learning WPF. Can we change other WPF object's property when an WPF object's property if 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 Trigger but I don't know how to do it.

P.S. If possible, I want to do it only in XAML.

+1  A: 

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, like

public class BoolToBrushConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool isChecked = (bool)value;

        string[] colours = parameter.ToString().Split(':');
        if (isChecked)
            return new BrushConverter().ConvertFromString(colours[0]);
        return new BrushConverter().ConvertFromString(colours[1]);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

then 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:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <local:BoolToBrushConverter x:Key="boolToBrushConverter" />
    </Window.Resources>

    <StackPanel Height="250">
        <ToggleButton Name="toggleButton" Height="32" Content="Green" />

    <TextBox
        Background="{Binding ElementName=toggleButton, Path=IsChecked, Converter={StaticResource boolToBrushConverter}, ConverterParameter=Green:White}" />
    </StackPanel>
</Window>

UPDATE: As per Ivan's excellent suggestion - have updated to show how you can pass parameters through to the Converter from the XAML...

IanR
Beat me to it! One suggestion: make the brushes properties on the BoolToBrushConverter, so that they can be set in XAML rather than being hardwired into the converter code. I realise you want to keep it simple for tutorial purposes, but I think this is a technique worth showing!
itowlson
I have tried it... and Wow, now I begin to understand why WPF is powerful on data binding. It's so simple and yet, flexible. Thanks a lot.
Ni'am