使用的BindingSource绑定到嵌套属性 - 或者,使实体可绑定 [英] Using BindingSource to bind to Nested Properties - or, Making Entities Bindable

查看:321
本文介绍了使用的BindingSource绑定到嵌套属性 - 或者,使实体可绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于使用的BindingSource绑定到嵌套属性 - 或者,使实体可绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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