小编Dav*_*ing的帖子

.NET WinForms INotifyPropertyChanged在更改一个绑定时更新所有绑定.更好的方法?

在Windows窗体应用程序中,触发INotifyPropertyChanged的属性更改将导致窗体从绑定对象读取每个属性,而不仅仅是属性已更改.(参见下面的示例代码)

这似乎是荒谬的浪费,因为界面需要更改属性的名称.它在我的应用程序中导致大量计时,因为某些属性getter需要执行计算.

如果没有更好的方法,我可能需要在我的getter中实现某种逻辑来丢弃不必要的读取.

我错过了什么吗?有没有更好的办法?不要说使用不同的演示技术 - 我在Windows Mobile上这样做(虽然行为也发生在完整的框架上).

这里有一些玩具代码来演示这个问题.单击该按钮将导致即使一个属性已更改,也会填充两个文本框.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Example
{
public class ExView : Form
{
    private Presenter _presenter = new Presenter();
    public ExView()
    {
        this.MinimizeBox = false;

        TextBox txt1 = new TextBox();
        txt1.Parent = this;
        txt1.Location = new Point(1, 1);
        txt1.Width = this.ClientSize.Width - 10;
        txt1.DataBindings.Add("Text", _presenter, "SomeText1");

        TextBox txt2 = new TextBox();
        txt2.Parent = this;
        txt2.Location = new Point(1, 40);
        txt2.Width = this.ClientSize.Width - 10;
        txt2.DataBindings.Add("Text", _presenter, "SomeText2");

        Button …
Run Code Online (Sandbox Code Playgroud)

.net c# data-binding inotifypropertychanged winforms

24
推荐指数
1
解决办法
2万
查看次数