如何使用属性管理器刷新通知控件值

本文关键字:通知 控件 刷新 管理器 何使用 属性 | 更新日期: 2023-09-27 18:35:06

我使用属性管理器将实体绑定到winform coltrols,但是当我将Entity设置为其他实体时,coltrol的值没有更新。

    private PropertyManager pm;
    txtItemId.DataBindings.Add("Value", Entity, "ItemId",true);
    txtItemName.DataBindings.Add("Text", Entity, "ItemName",true);
    pm=(PropertyManager)this.BindingContext[Entity];    
    void tsBtnQuery_Click(object sender, EventArgs e)
    {
        Table2QueryForm frm=new Table2QueryForm();          
        frm.ShowDialog();
        if(frm.Tag!=null)
        {
            Entity=(Table2)frm.Tag; 
        }
    }

如何使用属性管理器刷新通知控件值

一个简单的解决方案是使用 BindingSource 类,下面是项目的示例。

在表格中声明:

public BindingSource Source { get; set; }

表单加载事件中:

private void FormLoad(object sender, EventArgs e)
{
    // I assume that Entity is a variable of type Table2, 
    // if not then change Table2 to any other class
    Source = new BindingSource(typeof(Table2), null);
    txtItemId.DataBindings.Add("Value", Source, "ItemId", true, DataSourceUpdateMode.OnPropertyChanged);
    txtItemName.DataBindings.Add("Text", Source, "ItemName", true, DataSourceUpdateMode.OnPropertyChanged);
}

句柄按钮单击事件:

void tsBtnQuery_Click(object sender, EventArgs e)
{
    Source.DataSource = (Table2)frm.Tag;
}