RazorEater
RazorEater

Reputation: 265

Binding a property value to comparation of two variable values

In my WPF application i'm trying to bind my IsEnabled value of a control to the result of a comparation of two values.

Take val1 and val2 if val1 == val2 then IsEnabled should be true, otherwise it should be false

val1 and val2 can both change during the application

What would be the best way to do this?

Upvotes: 0

Views: 766

Answers (1)

I would definitely use a converter. To do this you need to implement the IMultiValueConverter. Here is an example.

Converter:

public class MyConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        return ( (int)values[ 0 ] == (int)values[ 1 ] );
    }

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

    #endregion
}

And create the IsEnabled-multibinding using the converter. Xaml:

<Window x:Class="WpfApplication61.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication61">

    <Window.Resources>
        <local:MyConverter x:Key="myConverter" />
    </Window.Resources>

    <Grid>
        <Grid.IsEnabled>
            <MultiBinding Converter="{StaticResource myConverter}">
                <Binding Path="Value1" />
                <Binding Path="Value2" />                                  
            </MultiBinding>
        </Grid.IsEnabled>

        <Button Content="Just a button" Width="75" Height="30" />
    </Grid>
</Window>

And the code-behind:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Random random;

    private int m_Value1;
    public int Value1
    {
        get
        {
            return m_Value1;
        }
        set
        {
            if ( m_Value1 == value )
            {
               return;
            }

            m_Value1 = value;
            NotifyPropertyChanged();
        }
    }

    private int m_Value2;
    public int Value2
    {
        get
        {
            return m_Value2;
        }
        set
        {
            if ( m_Value2 == value )
            {
                return;
            }

            m_Value2 = value;
            NotifyPropertyChanged();
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        random = new Random();

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    void timer_Elapsed( object sender, ElapsedEventArgs e )
    {
        Value1 = random.Next( 0, 2 );
        Value2 = random.Next( 0, 2 );
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged( [CallerMemberName] String propertyName = "" )
    {
        if ( PropertyChanged != null )
        {
            PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
        }
    }

    #endregion
}

Notice that the button is just to illustrate that the IsEnabled is toggled.

Happy coding :-)

Upvotes: 2

Related Questions