如何有一个用户控件的绑定属性随OnPropertyChanged工作 [英] How to have bindable properties of a UserControl which work with OnPropertyChanged

查看:302
本文介绍了如何有一个用户控件的绑定属性随OnPropertyChanged工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的用户控件(的WinForms),还有一些公共属性。当我使用这个控件,我想数据绑定与DataSourceUpdateMode设置这些属性为 OnPropertyChanged 。数据源是实现INotifyPropertyChanged的类。

I have a simple usercontrol (WinForms) with some public properties. When I use this control, I want to databind to those properties with the DataSourceUpdateMode set to OnPropertyChanged. The datasource is a class which implements INotifyPropertyChanged.

我意识到需要反对的属性创建绑定,我这样做。

I'm aware of the need to create bindings against the properties and I'm doing that.

我认为我的用户将必须实现一个接口,或属性将需要一些属性,或者类似的规定lines.But我的研究已经进行装饰空白。

I assumed that my usercontrol would have to implement an interface, or the properties would need to be decorated with some attribute, or something along those lines.But my research has come up blank.

应如何实现呢?此刻,我通过调用OnValidating()在我的用户只要属性发生变化,但看起来不正确这样做。

How should this be accomplished? At the moment I'm doing it by calling OnValidating() in my usercontrol whenever a property changes, but that doesn't seem right.

我能得到验证,如果我设置的CausesValidation为true的用户控件发生,但是这不是我很有用。我需要为它的变化,以验证各子属性。

I can get validation to happen if I set the CausesValidation to true on the usercontrol, but that's not very useful to me. I need to validate each child property as it changes.

请注意,这是一个的WinForms 的局面。

Note this is a WinForms situation.

编辑:显然我没有天赋的解释,所以希望这将澄清我在做什么。这是一个简化的例子:

Evidently I have no talent for explanation so hopefully this will clarify what I'm doing. This is an abbreviated example:

// I have a user control
public class MyControl : UserControl
{
    // I'm binding to this property
    public string ControlProperty { get; set; }

    public void DoSomething()
    {
        // when the property value changes, the change should immediately be applied 
        // to the bound datasource
        ControlProperty = "new value";

        // This is how I make it work, but it seems wrong
        OnValidating();         
    }
}

// the class being bound to the usercontrol
public class MyDataSource : INotifyPropertyChanged
{
    private string sourceProperty;
    public string SourceProperty
    {
        get { return sourceProperty; }
        set
        {
            if (value != sourceProperty)
            {
                sourceProperty = value;
                NotifyPropertyChanged("SourceProperty");
            }
        }
    }

    // boilerplate stuff
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

public class MyForm : Form
{
    private MyControl myControl;
    public MyForm()
    {
        // create the datasource 
        var dataSource = new MyDataSource() { SourceProperty = "test" };

        // bind a property of the datasource to a property of the usercontrol
        myControl.DataBindings.Add("ControlProperty", dataSource, "SourceProperty",
            false, DataSourceUpdateMode.OnPropertyChanged); // note the update mode
    }
}



(我已经试过这使用BindingSource的,但结果是一样的。)

(I have tried this using a BindingSource, but the result was the same.)

现在我希望发生的是,当MyControl.ControlProperty的值更改,更改会立即传播到数据源(该MyDataSource实例)。要做到这一点我叫OnValidating()在该用户更改属性之后。如果我不这样做,我要等到验证获得由焦点变化,这是OnValidation更新模式的等价物,而不是所期望的OnPropertyUpdate验证方式触发。我只是不喜欢改变的属性值后调用OnValidating()是做正确的事情,即使这(种)的作品。

Now what I want to happen is that when the value of MyControl.ControlProperty changes, the change is immediately propagated to the datasource (the MyDataSource instance). To achieve this I call OnValidating() in the usercontrol after changing the property. If I don't do that, I have to wait until validation gets triggered by a focus change, which is the equivalent of the "OnValidation" update mode, rather than the desired "OnPropertyUpdate" validation mode. I just don't feel like calling OnValidating() after altering a property value is the right thing to do, even if it (kind of) works.

我说得对不对在假设调用OnValidating()是的不可以以正确的方式做到这一点?如果是这样,我怎么通知ControlProperty变化的数据源?

Am I right in assuming the calling OnValidating() is not the right way to do this? If so, how do I notify the datasource of the ControlProperty change?

推荐答案

我觉得我有这个想通了。我不知道如何更改通知从控件绑定数据源发送的。

I think I've got this figured out. I didn't understand how change notifications were sent from control to bound datasource.

是,号召OnValidating()是错误的方式。

Yes, calling OnValidating() is the wrong way.

从我所拼凑起来,有两种方法控制可以通知物业已经改变了数据源。

From what I've pieced together, there are two ways a control can notify the datasource that a property has changed.

一个办法就是要实现INotifyPropertyChanged控制。我从来没有从控制端做之前,我想只有数据源端的结合必须实现它。

One way is for the control to implement INotifyPropertyChanged. I had never done this from the control side before, and I thought only the datasource side of the binding had to implement it.

当我实现INotifyPropertyChanged的在我的用户控制,并提出在适当的时候PropertyChanged事件,它的工作。

When I implemented INotifyPropertyChanged on my user control, and raised the PropertyChanged event at the appropriate time, it worked.

第二种方式是控制,以提高每个属性的特定更改事件。本次活动必须遵循的命名约定:<&PROPERTYNAME GT;变更

The second way is for the control to raise a specific change event for each property. The event must follow the naming convention: <propertyname>Changed

例如。对于我的例子这将是

e.g. for my example it would be

公共事件的EventHandler ControlPropertyChanged

如果我的财产被称为富,这将是 FooChanged

If my property was called Foo, it would be FooChanged.

我没有注意到在MSDN的relavent部分的文档,在那里说:

I failed to notice the relavent part of the MSDN documentation, where it says:

有关发生在
之间的绑定更改通知一个绑定的客户端和
的数据源,您绑定的类型应该
两种:

For change notification to occur in a binding between a bound client and a data source, your bound type should either:

实现INotifyPropertyChanged
接口(首选)

Implement the INotifyPropertyChanged interface (preferred).

为绑定类型的每个
属性发生变化的事件。

Provide a change event for each property of the bound type.

这第二种方式是所有现有的WinForms控件是如何工作的,所以这是我现在怎么做。我使用INotifyPropertyChanged的我的数据源,但我养我的控制变更事件。这似乎是常规方式

This second way is how all existing WinForms controls work, so this is how I'm doing it now. I use INotifyPropertyChanged on my datasource, but I raise the Changed events on my control. This seems to be the conventional way.

这篇关于如何有一个用户控件的绑定属性随OnPropertyChanged工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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