绑定到另一个类中的属性的文本字段不会更新

发布于 2024-11-28 12:55:21 字数 1263 浏览 1 评论 0原文

我有一个 Windows 窗体“MyForm”,其文本框绑定到另一个类“MyData”中的属性。数据源更新模式设置为“属性更改时”

我使用 VisualStudio IDE。它为绑定创建了以下代码

this.txtYield.DataBindings.Add(new Binding("Text", this.BindingSourceMyDataClass, "PropertyInMyDataClass", true, DataSourceUpdateMode.OnPropertyChanged));

在表单构造函数中,在初始化值之后添加了代码以将 MyData 类绑定到表单

myDataClassInstantiated = new MyDataClass();
BindingSourceMyDataClass.DataSource = myDataClassInstantiated;

INotifyProperty 接口已实现:

public double PropertyInMyDataClass
{
    get { return _PropertyInMyDataClass; }
    set
    {
        if (!Equals(_PropertyInMyDataClass, value))
        {
            _PropertyInMyDataClass = value;
            FirePropertyChanged("PropertyInMyDataClass");
        }
    }
}

后台工作人员用于运行计算并更新属性“PropertyInMyDataClass”

我预计当后台工作人员完成时,表单上的文本框会自动更新。这没有发生

如果我手动将值从属性复制到表单文本框,该值会正确显示

this.txtYield.Text = String.Format("{0:F0}", myDataClassInstantiated.PropertyInMyDataClass);

我尝试将 Refresh() 和 Update() 添加到 MyForm.MyBackgroundWorker_RunWorkerCompleted 方法,但数据仍然不显示神清气爽。

如果我稍后运行不同的后台工作程序来更新同一表单上的不同文本框,则绑定到 PropertyInMyDataClass 的文本框会更新,

我将不胜感激,这些建议将帮助我理解和解决此数据绑定问题

I have a windows form 'MyForm' with a text box that is bound to a property in another class 'MyData'. The Data source update mode is set to "On Property Change"

I used the VisualStudio IDE. It created the following code for the binding

this.txtYield.DataBindings.Add(new Binding("Text", this.BindingSourceMyDataClass, "PropertyInMyDataClass", true, DataSourceUpdateMode.OnPropertyChanged));

In the form constructor, after initialize values code was added to bind the MyData Class to the form

myDataClassInstantiated = new MyDataClass();
BindingSourceMyDataClass.DataSource = myDataClassInstantiated;

The INotifyProperty Interface has been implemented:

public double PropertyInMyDataClass
{
    get { return _PropertyInMyDataClass; }
    set
    {
        if (!Equals(_PropertyInMyDataClass, value))
        {
            _PropertyInMyDataClass = value;
            FirePropertyChanged("PropertyInMyDataClass");
        }
    }
}

A background worker is used to run the calculations and update the property 'PropertyInMyDataClass'

I expected that the text box on the form would update automatically when the background worker completed. That didn't happen

If I manually copy assign the value from the property to the form text box, the value is displayed properly

this.txtYield.Text = String.Format("{0:F0}", myDataClassInstantiated.PropertyInMyDataClass);

I tried to add Refresh() and Update() to the MyForm.MyBackgroundWorker_RunWorkerCompleted method, but the data still is not refreshed.

If I later run a different background worker that updates different text boxes on the same form, the text box bound to PropertyInMyDataClass gets updated

I would appreciate suggestions that will help me to understand and resolve this databinding problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-12-05 12:55:21

问题来自几个角度。如果您在后台线程上运行该进程,则后台线程无法直接访问表单上的控件(位于不同的线程中),否则您将收到异常。您也不能期望 UI 线程根据后台线程中的状态进行更新,除非您将其连接起来以执行此操作。为此,您需要在主 UI 线程上调用委托。

将此代码(修改它以使用您想要的任何值类型更新您想要的任何控件)放在 UI 表单上。

 public void UpdateOutput(string text)
     {
         this.Invoke((MethodInvoker) delegate {
           lstOutput.Items.Add(text);
         });
     }

然后您可以在后台工作线程中调用此函数。 (假设您的后台进程函数位于相同的表单中,您可以直接调用它),如果没有,那么您将需要对后台进程运行所在的类中的 UI 表单的引用。

The problem comes from a couple of angles. If you are running the process on a background thread, the background thread cannot directly access a control on your form (which lives in a different thread) directly, otherwise you will get an exception. You also cannot expect the UI thread to update based on states in the background thread, unless you wire it up to do so. In order to do this, you need to invoke a delegate on the main UI thread..

Place this code (modify it to update whatever control you want with whatever value type you want) on the UI form.

 public void UpdateOutput(string text)
     {
         this.Invoke((MethodInvoker) delegate {
           lstOutput.Items.Add(text);
         });
     }

Then you can call this function in your background worker thread. (assuming your background process function lives in the same form, you can call it directly), if not then you will need a reference to the UI form in the class that the background process runs in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文