当我将一个新的对象实例分配给绑定变量时,数据绑定不起作用 [英] Data Binding doesn't work when I assign a new object instance to the bound variable

查看:95
本文介绍了当我将一个新的对象实例分配给绑定变量时,数据绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我使用C#WinForms(目标.NET 4.5.2)绑定到窗体上的控件。我已经实现了 INotifyPropertyChanged ,当我修改此对象的属性时,它将按预期在Form的控件上更新。但是,当我将此对象的实例更改为新实例时,即使我尝试修改特定的属性,它也不再更新控件。

  class User:INotifyPropertyChanged 
{
string _name =;

public string Name
{
get {return _name; }
set {_name = value; OnPropertyChanged(Name); }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(property));
}

public User(){}

public User(string name)
{
Name = name;
}

public User(User otherUser)
{
Name = otherUser.Name;
}
}

在表单上我有

 用户currentUser =新用户(示例名称); 
lblName.DataBindings.Add(Text,currentUser,Name);

哪些更新正确。我可以使用 currentUser.Name =Blahblah; 更改名称,它可以正常工作。当我尝试调用 currentUser = new User(New Name); 时,无论我做什么都不会再更新。



我的理解是,对象的具体实例是控件绑定到的对象。我的主要目标是不必手动通过每个物体的较大物体,并且手动更改每一次我想要更改实例的一切。



我的问题是:是吗一种更改对象实例的方法,而不删除对控件的绑定?

解决方案

为了获得所需的行为,您不应该直接绑定到具体的实例,就像你目前一样:

  lblName.DataBindings.Add(Text,currentUser,Name); 

相反,您需要一些中介。最简单的方法是为此目的使用 BindingSource 组件。



添加一个 BindingSource 字段的形式:

  private BindingSource dataSource; 

然后初始化它并将控件绑定一次(通常以事件):

  dataSource = new BindingSource {DataSource = typeof(User)}; 
lblName.DataBindings.Add(Text,dataSource,Name);
// ...

现在随时你想绑定到一个用户实例,您只需将其分配给 BindingSource DataSource 属性:



初始化:

  dataSource。 DataSource = new User(Example Name); 

以后:

  dataSource.DataSource = new User(New Name); 


I have an object that I have bound to a control on a form using C# WinForms (targetting .NET 4.5.2). I have implemented INotifyPropertyChanged, and when I modify a Property of this object, it updates on the Form's control as expected. However, when I change this object's instance to a new instance, it will no longer update the control, even if I try to modify a specific Property.

class User : INotifyPropertyChanged
{
    string _name = "";

    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }

    public User() { }

    public User(string name)
    {
        Name = name;
    }

    public User(User otherUser)
    {
        Name = otherUser.Name;
    }
}

and on the Form I have

User currentUser = new User("Example Name");
lblName.DataBindings.Add("Text", currentUser, "Name");

which updates properly. I can change name using currentUser.Name = "Blahblah"; and it works fine. When I try to call currentUser = new User("New Name");, it will no longer update no matter what I do.

It is my understanding that the specific instance of the object is what the controls are bound to. My primary goal is to not have to manually go through each Property of larger objects and manually change everything over every single time I want to change instances.

My question: is there a way to change instances of an object without removing the binding to the control?

解决方案

To get the desired behavior, you should not bind directly to the concrete instance like you do currently:

lblName.DataBindings.Add("Text", currentUser, "Name");

Instead, you need some intermediary. The easiest is to use the BindingSource component for that purpose.

Add a BindingSource field to the form:

private BindingSource dataSource;

Then initialize it and bind the controls to it one time (usually in form Load event):

dataSource = new BindingSource { DataSource = typeof(User) };
lblName.DataBindings.Add("Text", dataSource, "Name");
// ...

Now anytime you want to bind to a User instance, you simply assign it to the DataSource property of the BindingSource:

initial:

dataSource.DataSource = new User("Example Name");

later:

dataSource.DataSource = new User("New Name"); 

这篇关于当我将一个新的对象实例分配给绑定变量时,数据绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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