【问题标题】:CheckBox binding not updating sourceCheckBox 绑定不更新源
【发布时间】:2016-12-08 16:50:05
【问题描述】:

在通过 DataTrigger 更改 CheckBox.IsChecked 后,CheckBox.IsChecked 绑定的源值没有改变。

我有简单的 ViewModel

public class ViewModel : INotifyPropertyChanged
    {    
        private bool check1;    
        public bool Check1
        {
            get { return check1; }
            set { check1 = value; NotifyPropertyChanged(); }
        }    

        private bool check2;    
        public bool Check2
        {
            get { return check2; }
            set { check2 = value; NotifyPropertyChanged(); }
        }

        #region Notify
        ...    
        #endregion
    }

和简单的 XAML

<StackPanel Grid.Row="1">
        <CheckBox Content="Check1">
            <CheckBox.Style >
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsChecked" Value="{Binding Check1, Mode=TwoWay}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Check2}" Value="True">
                            <Setter Property="IsChecked" Value="True"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>

        <CheckBox Content="Check2" IsChecked="{Binding Check2}"/>

        <TextBlock Text="{Binding Check1}" Name="uiCheckValue1"/>
        <TextBlock Text="{Binding Check2}" Name="uiCheckValue2"/>
    </StackPanel>

当我检查 CheckBox2 时, CheckBox1 被选中,但源没有更新。如何让它更新源?

【问题讨论】:

  • 试试“{Binding Check1, Mode=TwoWay]”

标签: wpf data-binding datatrigger


【解决方案1】:

您的代码似乎有误。您应该正确发出 PropertyChanged 事件以更新视图。 检查以下代码:

public class ViewModel : INotifyPropertyChanged
{    
    private bool check1;    
    public bool Check1
    {
        get { return check1; }
        set 
        { 
            check1 = value; 
            PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
        }
    }    

    private bool check2;    
    public bool Check2
    {
        get { return check2; }
        set 
        { 
            check2 = value;
            PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
        }
    }

    #region Notify
    ...    
    #endregion
}

还将绑定更改为TwoWay

<<CheckBox Content="Check2" IsChecked="{Binding Check2, Mode=TwoWay}"/>

<DataTrigger Binding="{Binding Check2, Mode=TwoWay}" Value="True">
<Setter Property="IsChecked" Value="True" />
</DataTrigger>

【讨论】:

    【解决方案2】:

    原因很容易说:如果手动将IsChecked 设置为True,就会失去绑定。
    删除 DataTrigger 并像这样更改您的 ViewModel:

    public bool Check2{
        get { return check2; }
        set { 
              check2 = value; 
              if (value == true) Check1 = true;
              NotifyPropertyChanged(); 
            }
     }
    

    【讨论】:

      【解决方案3】:

      有点晚了,但如果你改变

      <CheckBox Content="Check2" IsChecked="{Binding Check2}"/>
      

      <CheckBox Content="Check2" IsChecked="{Binding Check2, UpdateSourceTrigger=PropertyChanged}"/>
      

      你可以摆脱所有背后的代码。复选框的默认设置类似于 LostFocus,这完全是令人讨厌的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-08
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多