将 WPF 控件绑定到多个源(不是传统的多重绑定)

发布于 2024-09-16 17:02:35 字数 323 浏览 6 评论 0原文

我正在尝试做一些数据绑定魔法。我有一个发货视图,其中列出了发货,并提供了列表上的过滤和排序功能。过滤器字符串框、交付状态过滤器(复选框)和排序单选按钮均以数据方式绑定到 ViewModel 中的属性。我想添加保存状态的功能,并且我选择通过在 xml 文档中保存控件状态来实现此目的。以前我已经这样做过,没有什么问题,使用数据绑定来来回读取/写入值。

然而,现在我有一个困惑。我的过滤器控件当前数据绑定到 ViewModel 中的项目。我可以编写代码,在加载时将其数据绑定从 xml 更改为 ViewModel,反之亦然,但这会很混乱。

是否有一种机制可以用来实现绑定到两个相同源并同时更新它们的能力?

I am trying to do some databinding magic. I have a Shipments view that lists shipments, and provides filtering and ordering ability on the list. The filter string box, Delivery Status filters (checkboxes) and Ordering Radiobuttons are databound to properties in the ViewModel. I want to add the ability to save state and I have elected to do this by saving control states in an xml document. Previously I have done this before with little problem, using databinding to just read/write the values back and forth.

However, now I have a quandry. My filter controls are currently databound to items in the ViewModel. i can write code that changes their databinding from the xml to the ViewModel on load and vice versa, but that would be messy.

Is there a mechanism in place that I can use to achieve the ability to bind to two equal sources and have them updated at the same time?

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

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

发布评论

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

评论(2

汐鸠 2024-09-23 17:02:35

这听起来像是对视图模型的关注。

为什么不将保存的值加载到视图模型中,并让视图模型决定要公开哪些数据?

那么视图就不必关心管理数据。

This sounds like a concern for the view model.

Why not load the saved values into the view model, and have the view model decide what data to expose?

Then the view doesn't have to be concerned with managing data.

墟烟 2024-09-23 17:02:35

据我所知,没有一个。

我的观点:无论如何我真的不会这样做 - 如果您的数据上下文是视图模型,并且视图模型具有过滤器的属性,那么您几乎肯定应该保留并检索相关的视图模型状态以保持过滤器的状态。尝试保存 controlstate,然后检索它、设置它,并根据新的 controlstate 设置视图模型,听起来需要更多工作,而且更容易出现错误。

None that I'm aware of.

My opinion: I really wouldn't do this anyway - if your datacontext is the viewmodel, and the viewmodel has properties for the filter, you almost certainly should be persisting and retrieving the relevant viewmodel state to keep the state of the filters. Trying to save controlstate, then retrieve it, set it, and set the viewmodel based on the new controlstate sounds like a lot more work and much more prone to bugs.

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