使用的BindingSource绑定到嵌套属性 - 或者,使实体可绑定绑定、嵌套、实体、属性

由网友(我帅我随意)分享简介:绑定到一个嵌套的属性是很容易的:Binding to a nested property is easy enough:checkBox1.DataBindings.Add(new Binding("Checked", bindingSource, "myProperty")); //Normal binding...

绑定到一个嵌套的属性是很容易的:

Binding to a nested property is easy enough:

checkBox1.DataBindings.Add(new Binding("Checked", bindingSource, "myProperty")); //Normal binding
checkBox2.DataBindings.Add(new Binding("Checked", bindingSource, "myProperty.innerProperty")); //Nested property

然而,当 myProperty.innerProperty 被改变,不引发事件。 - BindingSource的是永远不会更改的通知

However, when myProperty.innerProperty is changed, no events are raised - the BindingSource is never notified of the change.

我已经阅读该解决方案是确保当 innerProperty 对象提出了一个的PropertyChanged 事件时, myProperty的包含类 innerProperty 捕获的事件,也引发了自己的的PropertyChanged 事件。 的

I've read that the solution is to "make sure that when the innerProperty object raises the PropertyChanged event, the MyProperty class that contains innerProperty captures the event and also raises a PropertyChanged event of its own."

然而,实体框架不为我做到这一点,和我宁愿不通过每个类和线了一个自定义的方法,每一个导航属性​​的每个实例,只是为了让我的班可绑定。 有一个体面的解决办法,使实体绑定?

However, entity framework does not do this for me, and I'd rather not go through every instance of every class and wire-up a custom method to every navigation property, just to make the my classes bindable. Is there a decent workaround to make entities bindable?

推荐答案

您必须实现INotifyPropertyChanged的类。

You have to implement INotifyPropertyCHanged on your class.

您的属性应该是这个样子。

Your property should look something like this.

private bool _checked;
    public bool Checked
    {
        get { return _checked; }
        set
        {
            if (value != _checked)
            {
                _checked = value;
                OnPropertyChanged("Checked");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyCHanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我不知道这是否适用于的WinForms。它适用于WPF和Silverlight。

I'm not sure if this works for winforms. It works for WPF and Silverlight.

阅读全文

相关推荐

最新文章