将属性值绑定到两个变量值的比较

时间:2014-05-27 08:36:46

标签: c# wpf data-binding controls

在我的WPF应用程序中,我试图将控件的IsEnabled值绑定到两个值的比较结果。

取val1和val2 如果val1 == val2则IsEnabled应为true,否则应为false

val1和val2都可以在应用程序中更改

最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

我肯定会使用转换器。为此,您需要实现IMultiValueConverter。这是一个例子。

转换器:

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
}

使用转换器创建IsEnabled-multibinding。 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>

代码隐藏:

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
}

请注意,该按钮仅用于说明IsEnabled已切换。

快乐编码: - )