简体   繁体   English

WPF:条件绑定

[英]WPF: Conditional Binding

If my CheckBox is checked, I would like to disable the ComboBox and select "Yes". 如果我的CheckBox被选中,我想禁用ComboBox并选择“是”。 This works, what I would like to do is make this conditional such that when I uncheck the CheckBox MyBooleanProperty is not updated and "Yes" is still selected. 这行得通,我想做的是使此条件为条件,以便当我取消选中CheckBox MyBooleanProperty时不更新,并且仍然选择“是”。 The ComboBox itself should enable allowing me to choose "No" if desired. ComboBox本身应允许我根据需要选择“否”。

<CheckBox Name="chkDefault" IsChecked="{Binding MyBooleanProperty, Mode=OneWayToSource}" />
<ComboBox IsEnabled="{Binding IsChecked, ElementName=chkDefault, Converter={StaticResource InverseBooleanConverter}}">
    <ComboBoxItem Content="No" />
    <ComboBoxItem Content="Yes" IsSelected="{Binding MyBooleanProperty}" />
</ComboBox>

In the past, I've done this with multiple properties(see below) but I'm wondering if there isn't something simpler than can be done in XAML that I am not aware of. 过去,我已经使用多个属性完成了此操作(请参见下文),但我想知道是否有比我不知道的XAML更简单的方法。

The View... 风景...

<CheckBox IsChecked="{Binding MyBooleanProperty, Mode=OneWayToSource}" />
<ComboBox IsEnabled="{Binding MyBooleanProperty, Converter={StaticResource InverseBooleanConverter}}">
    <ComboBoxItem Content="No" />
    <ComboBoxItem Content="Yes" IsSelected="{Binding MyOtherBooleanProperty}" />
</ComboBox>

And the ViewModel... 还有ViewModel ...

public bool MyBooleanProperty
{
    get { return _myBooleanProperty; }
    set
    {
        if (_myBooleanProperty != value)
        {
             _myBooleanProperty = value;

             if (_myBooleanProperty)
                MyOtherBooleanProperty = true;

             base.OnPropertyChanged("MyBooleanProperty");
        }
    }
}

public bool MyOtherBooleanProperty
{
    get { return _myOtherBooleanProperty; }
    set
    {
        if (_myOtherBooleanProperty != value)
        {
             _myOtherBooleanProperty = value;

             base.OnPropertyChanged("MyOtherBooleanProperty");
        }
    }
}

You are describing the logic you would like your view to have. 您正在描述希望您的视图具有的逻辑。 To me, at least, that generally means that such logic belongs in the view model. 至少对我而言,这通常意味着这种逻辑属于视图模型。

But, I would not do the bindings the way you have. 但是,我不会像您那样进行绑定。 I would use something like this: 我会用这样的东西:

<CheckBox Name="chkDefault" IsChecked="{Binding UseDefault}" />
<ComboBox IsEnabled="{Binding EnableComboBox}}" SelectedItem="{Binding SelectedComboItem}">
    <ComboBoxItem Content="No" />
    <ComboBoxItem Content="Yes" />
</ComboBox>

Then in your view model, you have UseDefault , EnableComboBox , and SelectedComboItem properties which clearly state what the behavior should be. 然后,在视图模型中,您将具有UseDefaultEnableComboBoxSelectedComboItem属性,这些属性清楚地说明了行为应该是什么。

Also, by doing this in the view model, you are making the behavior unit testable. 另外,通过在视图模型中执行此操作,可以使行为单元可测试。 :) :)

I would control the logic for this in the viewmodel, and not try to imply the logic via bindings. 我将在视图模型中控制此逻辑,而不尝试通过绑定暗示逻辑。 The View should just care that you've bound the combobox selection to a backing data member (two-way), and that the checkbox's value is bound too. 视图应该只关心您已将组合框选择绑定到支持数据成员(双向),并且复选框的值也已绑定。

    public bool IsOverrideProperty
    {
        get { return _IsOverrideProperty; }
        set
        {
            if (_IsOverrideProperty != value)
            {
                _IsOverrideProperty = value;

                if (_IsOverrideProperty)
                    Selection = Options[0];

                base.OnPropertyChanged("IsOverrideProperty");
            }
        }
    }

    bool _IsOverrideProperty;

    public string Selection
    {
        get { return _Selection; }
        set
        {
            if (_Selection != value)
            {
                _Selection = value;

                base.OnPropertyChanged("Selection");
            }
        }
    }

    string _Selection;

    public string[] Options = new[] { "Yes", "No" };

The View: 风景:

<CheckBox IsChecked="{Binding IsOverrideProperty}" />
<ComboBox IsEnabled="{Binding IsOverrideProperty, Converter={StaticResource InverseBooleanConverter}}"
          SelectedValue="{Binding Selection}" ItemsSource="{Binding Options}">
</ComboBox>

Options could be in an observablecollection, but this probably works okay. 选项可能在可观察的集合中,但这可能行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM