将 WPF ComboBox SelectedValue 属性绑定到多个对象

发布于 2024-07-27 20:47:38 字数 445 浏览 4 评论 0原文

假设我有一个客户列表(List),每个客户都有一个 State-property。

是否可以绑定 ComboBox 的 SelectedValue,以便它显示(具有 SelectedValue),例如“WA”(如果所有客户都将 WA 作为其状态)?

如果客户具有不同的状态值,则不会显示任何内容。 但如果我随后选择“或”,则所有客户都应将其“状态”设置为该值。

我通过 ValueConverter 实现了这一点,其中使用 Convert 方法返回 State 属性的值(如果它们全部相同),否则返回 null。 在此方法中,值参数是一个列表...

但是在 ConvertBack 方法中,我无权访问 List 对象,但可以在设置绑定时将参数对象设置为 XAML 中的 List 对象,然后设置手动将国家财产分配给所有客户,但这感觉不对!

还有其他方法可以做到这一点吗?

Let's say I have a list of Customers (List) and each Customer has a State-property.

Is it possible to bind the SelectedValue of a ComboBox, so that it shows (has a SelectedValue of) e.g. "WA" if all Customers have WA as their State?

If the customers have different State-values nothing is shown.
But if I then choose OR, then all Customers should have their State set to this value.

I've accompliced it through a ValueConverter, where I use the Convert method to return the value of the State-property if they're all the same, and null if not. In this method the value-parameter is a List...

But in the ConvertBack method I don't have access to the List object, but could set the parameter object to the List object i XAML when setting up the binding, and then setting the State-property manually on all Customers, but that just doesn't feel right!

Is there any other way to do this?

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

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

发布评论

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

评论(1

比忠 2024-08-03 20:47:38

好吧 - 忘记用我的解决方案更新这个任务。

在我的 ValueConverter 上,我有一个私有列表参数,如果所有选定的客户具有相同的状态属性,我将在其中添加它们。

然后在 ConvertBack 方法中,我只更新列表中所有客户对象的状态...

Well - forgot to update this questing with my solution.

On my ValueConverter I have a private List parameter, where I add all selected customers if they have the same State-property.

Then in the ConvertBack-method I just update the State of all the Customer-objects in the List...

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