动态绑定 DataRepeater (Microsoft.VisualBasic.PowerPacks)

发布于 2024-08-08 04:34:36 字数 1552 浏览 3 评论 0原文

我正在使用 DataRepeater 在屏幕上显示来自业务对象的数据。我使用 C# 中的 Windows 窗体来完成此任务。数据源在编译时不可用,因此我想在运行时绑定数据源。

这是简化的场景。我正在使用此业务类:

public class Product
{

    private double _price;
    public double Price 
    { 
        get
        {
            return _price;
        }
        set
        {
            _price = value;
        }
    }
}

我使用 VisualStudio 界面创建了一个 ProductDataSource 并将价格绑定到标签。现在,我用代码填充了中继器的数据源:

dataRepeater1.DataSource = _productDataAgent.GetProducts();

当我启动应用程序时,价格已正确填充在标签中。到目前为止,一切都很好。

现在我希望产品更新时价格标签也随之更新。 Visual Studio 界面帮助我,让我选择“数据源更新模式”。所以我选择“OnPropertyChanged”。

棘手的部分来了。 .NET 运行时如何知道价格属性已从后端更新。所以我修改我的业务类来实现 INotifyPropertyChanged。像这样:

public class Product : INotifyPropertyChanged
{
    private double _price;

    public double Price 
    { 
        get
        {
            return _price;
        }
        set
        {
            _price = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Price"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

问题是这不起作用。当我更新产品时,它意味着界面中未更新。当我调试并更改属性时,我发现 PropertyChanged 事件为 null,因此没有人在监听。

更深入地研究这个问题,我在 MSDN 上的 System.Windows.Forms.Binding 构造函数页面上发现了以下内容:

名为 PropertyNameChanged 的​​事件。

所以我尝试使用(自定义)PriceChanged 事件,但这不起作用。

我在这里做错了什么吗?我刚开始使用 WPF,所以这在 Windows 窗体中的工作方式可能有点不同?这是因为我在运行时绑定吗?

I am using a DataRepeater to show data from a business objects on the screen. I am using windows forms in C# to accomplish this. The datasource is not available at compile time so I want to bind the datasource at runtime.

Here is the simplified scenario. I'm using this business class:

public class Product
{

    private double _price;
    public double Price 
    { 
        get
        {
            return _price;
        }
        set
        {
            _price = value;
        }
    }
}

I have created a ProductDataSource with the VisualStudio interface and bound the price to a label. Now I filled the datasource of my repeater in code:

dataRepeater1.DataSource = _productDataAgent.GetProducts();

When I startup my application the prices are correctly filled in the labels. So far so good.

Now I want the price labels to be updated when the product is updated. The Visual Studio interface helps me, and let me choose a 'Data Source Update Mode'. So I choose "OnPropertyChanged".

Here comes the tricky part. How does the .NET runtime know that the price property is updated from the backend. So I modify my business class to implement INotifyPropertyChanged. Like this:

public class Product : INotifyPropertyChanged
{
    private double _price;

    public double Price 
    { 
        get
        {
            return _price;
        }
        set
        {
            _price = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Price"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

The problem is this doesn't work. When I update a product it remeanes un-updated in the interface. When I debug and change the property, I see that the PropertyChanged event is null so no one is listening.

Delving a little deeper in to the problem I found the following on the System.Windows.Forms.Binding Constructor page on MSDN:

An event named PropertyNameChanged.

So I tried using a (custom) PriceChanged event, but that did not work.

Am I doing something wrong here? I am comming from using WPF, so maybe this works a little different in Windows Forms? Is this because I am binding at runtime?

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

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

发布评论

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

评论(1

伊面 2024-08-15 04:34:36

杰普找到了解决方案。显然你不能简单地绑定到产品列表。您最初会看到产品,但当属性更改时它们不会更新。相反,您需要静态绑定到 BindingSource。只需使用 Visual Studio 创建一个对象数据源(在数据菜单中)。生成这样的代码:

private System.Windows.Forms.BindingSource beursProductDisplayBindingSource;
this.beursProductDisplayBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.dataRepeater1.DataSource = this.beursProductDisplayBindingSource;

现在您可以像这样动态绑定:

BindingSource productBinding = ((BindingSource)dataRepeater1.DataSource);
_productDataAgent.BeursProducts.ForEach(product => productBinding.Add(product));

现在,当像我一样在数据对象中实现 INotifyPropertyChanged 时,它的工作原理与预期一致。只是忘记了使用 WPF 时不需要的一步。

Jep found the sollution. Apparently you cannot simply bind to a list of products. You will see the products initially, but they will not be updated when a property is changed. Instead you need to statically bind to a BindingSource. Just create an object datasource using the Visual Studio (in the data menu). Code like this is generated:

private System.Windows.Forms.BindingSource beursProductDisplayBindingSource;
this.beursProductDisplayBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.dataRepeater1.DataSource = this.beursProductDisplayBindingSource;

Now you can dynamically bind like this:

BindingSource productBinding = ((BindingSource)dataRepeater1.DataSource);
_productDataAgent.BeursProducts.ForEach(product => productBinding.Add(product));

Now when implementing INotifyPropertyChanged in your data object like I did is works like expected. Just forgot one step which is not needed when using WPF.

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