Winforms中的双向数据绑定,在基类中实现了Inotify属性更改 [英] Two way databinding in winforms, Inotifypropertychanged implemented in base class

查看:78
本文介绍了Winforms中的双向数据绑定,在基类中实现了Inotify属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.Net 3.5,Winforms,Databinding

I use .Net 3.5, Winforms, Databinding

我有派生类,基类实现了IPropertychanged

I have derived classes, the base class implements IPropertychanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName) {
        var handler = this.PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

每次置业者致电:

    protected void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            IsDirty = true;
            this.RaisePropertyChanged(propertyName);
        }
    }

典型的房地产经纪人:

    public String LocalizationItemId {
        get {
            return _localizationItemId;
        }
        set {
             SetField(ref _localizationItemId, value, "LocalizationItemId");
        }
    }

属性与文本框的绑定方式

The way a property is bound to a textbox

        private DerivedEntity derivedEntity
        TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");

如果我以编程方式将文本分配给文本框,则文本框不会显示它。但是我可以手动编辑文本框。

If I programmatically assign text to the textbox, the textbox does not show it. But I can manually edit the textbox.

推荐答案

我知道现在回答还为时已晚,但是,如果在绑定应更改的情况下设置了事件,则可以解决此问题值,如果将其设置为属性值更改事件,则将解决您的问题。您可以通过这种方式完成

I know it is too late to answer, but this problem can be solved, if you set event when your binding should change value, if you set it on property value change event your problem will be solved. You can do this by this way

textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);

这篇关于Winforms中的双向数据绑定,在基类中实现了Inotify属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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