不会触发WPF DataBinding propertychanged事件

时间:2015-01-29 17:37:01

标签: wpf data-binding

型号:

public class Item
{
    public string ItemNo { get; set; }
    public double Qty { get; set; }
    public string Box { get; set; }
    public string SerialNo { get; set; }
}

视图模型:

 public Item ScanningItem 
    {
        get
        {
            return _scanningItem;
        }
        set
        {
            _scanningItem = value;

            RaisePropertyChanged("ScanningItem");
            if (AddItemCommand !=null)
                AddItemCommand.RaiseCanExecuteChanged();
        }
    }

查看:

                                                                                                                                                       

问题:更改文本框中的值,Item - > ScanningItem的PropertyChanged事件不会被触发。

在MVVMLight中,有一个功能,如果得到了PropertyChanged,Model可以通知ViewModel,有人知道如何使用该功能吗?

1 个答案:

答案 0 :(得分:0)

您提到了一个TextBox,可能是您绑定到对象的Property。这不起作用,因为您没有仅将对象的实例更改为属性的值。

在这种情况下,您有两个选项,您可以单独公开属性并触发PropertyChanged事件。或者,您可以在模型中实现INotifyPropertyChanged。

<强>视图模型

public string SerialNo
{
    get { return _scanningItem.SerialNo; }
    set
    {
        _scanningItem.SerialNo = value;
        RaisePropertyChanged("SerialNo");
    }
}
相关问题