WPF复选框IsChecked属性不会根据绑定值进行更改 [英] WPF checkbox IsChecked property does not change according to binding value

查看:179
本文介绍了WPF复选框IsChecked属性不会根据绑定值进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码:



xaml side:
我使用数据模板绑定项dataType1

 < DataTemplate DataType ={x:Type dataType1}> 
< WrapPanel>
< CheckBox IsChecked ={Binding Path = IsChecked,Mode = TwoWay}Command ={Binding Path = CheckedCommand} />
< TextBlock Text ={Binding Path = ItemName,Mode = OneWay}/>
< / WrapPanel>
< / DataTemplate>

然后我创建一个类型为dataType1的项的ComboBox

 < ComboBox Name =comboBoxItemsItemsSource = {Binding Path = DataItems,Mode = TwoWay}> 

这里是dataType1 diffinition: / p>

  class dataType1 {public string ItemName {get; set;} public bool IsChecked {get; set;}} 

该场景是我准备一个dataType1的列表,并将其绑定到ComboBox,ItemName显示无瑕疵,而CheckBox IsChecked值始终未被选中不管dataType1中的IsChecked的值如何。



在wpf中复选框中绑定IsChecked属性时需要特殊处理吗?



彼得·乐ung

解决方案

您在这里遇到的问题是 CheckBox 不知道 dataType1.IsChecked 的值何时更改。要修复它,请将您的dataType1更改为:

  class dataType1:INotifyPropertyChanged 
{
public string ItemName {得到;组;

private bool isChecked;
public bool IsChecked
{
get {return isChecked;
set
{
if(isChecked!= value)
{
isChecked = value;
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(IsChecked));
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}

所以现在,当属性值更改时,它会通知绑定它需要通过提高 PropertyChanged 事件来更新。



此外,还有更简单的方法可以避免你写得尽可能多的锅炉代码。我使用 Josh Smith的BindableObject


here is my code:

xaml side: I use a data template to bind with item "dataType1"

<DataTemplate DataType="{x:Type dataType1}">
    <WrapPanel>
        <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Command="{Binding Path=CheckedCommand} />
        <TextBlock Text="{Binding Path=ItemName, Mode=OneWay}" />
    </WrapPanel>
</DataTemplate>

then I create a ComboBox with item with type "dataType1"

<ComboBox Name="comboBoxItems" ItemsSource="{Binding Path=DataItems, Mode=TwoWay}">

and here is dataType1 difinition:

class dataType1{public string ItemName{get; set;} public bool IsChecked {get; set;}}

the scenario is I prepare a list of dataType1 and bind it to the ComboBox, ItemName display flawlessly while CheckBox IsChecked value is always unchecked regardless the value of "IsChecked" in dataType1.

Is special handling needed in binding IsChecked property in CheckBox in wpf?

Peter Leung

解决方案

The problem you're having here is that the CheckBox doesn't know when the value of dataType1.IsChecked changes. To fix that, change your dataType1 to:

class dataType1 : INotifyPropertyChanged
{ 
    public string ItemName { get; set; }

    private bool isChecked;
    public bool IsChecked 
    {
        get { return isChecked; }
        set 
        {
            if (isChecked != value)
            {
                isChecked = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

So now, when the property value is changed it will notify the binding that it needs to update by raising the PropertyChanged event.

Also, there are easier ways to do this that avoid you having to write as much boiler-plate code. I use BindableObject from Josh Smith.

这篇关于WPF复选框IsChecked属性不会根据绑定值进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆