解除绑定绑定源

发布于 2024-11-18 02:49:38 字数 468 浏览 6 评论 0原文

我在 Windows 窗体应用程序中使用绑定源来填充我视图中的一些文本框等。绑定工作正常,但我如何从我的对象中取消订阅绑定源?

bindingSource.DataSource = new Foo();//OK
bindingSource.DataSource = null;//Not ok

如果我尝试通过设置 data = null 取消绑定,则会出现异常:

System.ArgumentException:无法绑定 到属性或列栏 数据源。参数名称:dataMember

我不想删除与我的控件的所有绑定(我有很多),但希望只要绑定源没有数据就暂停绑定....

我找到了像这样的解决方法 bindingSource .DataSource = typeof(Foo); 但这是这样吗?

I am using a bindingsource in my windows forms application to populate some textboxes etc in my view. Binding works OK but how can I unsubscribe my bindingSource from from my object?

bindingSource.DataSource = new Foo();//OK
bindingSource.DataSource = null;//Not ok

If I try to unbind by setting data = null I get an exception:

System.ArgumentException : Cannot bind
to the property or column Bar on the
DataSource. Parameter name: dataMember

I don't want to remove all bindings to my controls (i have a lot) but would like to suspend binding as long as the bindingSource has no data....

I found a workaround like this bindingSource.DataSource = typeof(Foo); but is this THE way?

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

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

发布评论

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

评论(4

浊酒尽余欢 2024-11-25 02:49:38

typeof“解决方法”实际上是 Windows 窗体设计器在 PropertyGrid 中设置 BindingSource 的数据源并从“项目数据源”中选择类型时所做的事情。

查看 *.designer.cs 文件中为您的表单生成的代码。

我们在我们的一款产品中使用了这一“技巧”,并且多年来一直运作良好。

问候

The typeof "workaround" is actually what the windows forms designer does when you set the BindingSource's DataSource in the PropertyGrid, and select a type from "Project data sources".

Look at the generated code in the *.designer.cs file for your form.

We use this "trick" in one of our products, and it has worked well for many years now.

Regards

抱猫软卧 2024-11-25 02:49:38

我不知道 BindingSource 对象有 .Data 属性,但有一个 .DataSource 属性,可以将其设置为 null:

bindingSource.DataSource = null;

这会从数据。但是,查看 BindingSource.DataSource 的参考< /a>:

DataSource property             List results
----------------------------    -------------------------------------------
null with DataMember set        Not supported, raises ArgumentException.

如果您使用 DataMember,则无法将 DataSource 设置为 null,不会出现异常。

不幸的是,我不知道您的解决方法是否是正确的方法,但至少现在我们知道,当设置 DataMember 时,您不能简单地绑定到 null。

I am not aware of a .Data property for the BindingSource object, but there is a .DataSource property, which can be set to null:

bindingSource.DataSource = null;

This releases the binding source from the data. However, looking at the reference for BindingSource.DataSource:

DataSource property             List results
----------------------------    -------------------------------------------
null with DataMember set        Not supported, raises ArgumentException.

If you're using a DataMember, you can't set the DataSource to null without an exception.

Unfortunately I don't know whether your workaround is a proper way of doing it, but at least now we know that you can't simply bind to null when a DataMember is set.

南城旧梦 2024-11-25 02:49:38

mrlucmorin 给了你正确的答案。它正在发挥作用,并且是处理这种情况的正确方法。

但是,如果您的 DataSource 是 DataTable 类型,则它不会完全工作。在这种情况下,您可能需要在清空 BindingSource.DataSource 之前使用 bs.RaiseListChangedEvents = false;,并在分配新的 DataSource 后将其设置为 true。将其设置为 true 后,不要忘记使用 bs.ResetBindings(true); 重置绑定。

请注意,这可能会导致数据绑定控件中保留“旧”数据。

mrlucmorin gave you correct answer. It is working and it is the correct way of handling such situation.

However it won't quite work if your DataSource is of DataTable type. In such case you might want to play with bs.RaiseListChangedEvents = false; before nulling the BindingSource.DataSource, and set it to true after you assign new DataSource. Right after you set it to true, don't forget to reset the bindings with bs.ResetBindings(true);

Be aware that this might cause leaving your databound controls with 'old' data in them.

帅气尐潴 2024-11-25 02:49:38

当使用 typeof 作为 DataSource 的“空”值时,您可以像这样测试它:

private void BindingSource_DataSourceChanged(object sender, EventArgs e)
{
    DataSource dataSource = ((BindingSource)sender).DataSource;

    if (dataSource is Type t && t == typeof(MyModel))
    {
        lblEmpty.Visible = true;
        pnlDetails.Visible = false;
    }
    else
    {
        lblEmpty.Visible = false;
        pnlDetails.Visible = true;
    }
}

这样您就可以以简单的方式有条件地在 UI 中隐藏或显示“空”消息。

When using typeof as "empty" value for the DataSource, you can test for it like this:

private void BindingSource_DataSourceChanged(object sender, EventArgs e)
{
    DataSource dataSource = ((BindingSource)sender).DataSource;

    if (dataSource is Type t && t == typeof(MyModel))
    {
        lblEmpty.Visible = true;
        pnlDetails.Visible = false;
    }
    else
    {
        lblEmpty.Visible = false;
        pnlDetails.Visible = true;
    }
}

This way you can conditionally hide or show an "empty" message in the UI, in a simple way.

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