DataContextProxy silverlight 组合框 - 有更好的方法吗?
我正在使用 Dan Wahlin 解决了在发现这一发现之前的许多小时内看起来非常困难的问题。
我有一个绑定到 ObservableCollection
的组合框。该组合框还具有以下值:DisplayMemberPath=AccountNumber
和 SelectedValue={Binding SelectedAccount}
。然后,我有一个 DataForm,其 CurrentItem 属性也绑定到 SelectedAccount。在我的 DataForm 中,我有一些需要额外下拉列表的字段(AcctCode、UsageCode 等)。为了缓解在 DataForm 内部遇到的绑定问题,我实现了 datacontextproxy,允许我将 cbobox 的 ItemsSource 设置为 {Binding Source={StaticResource DataContextProxy}, Path=DataSource.AccountCodes}"
.
在偶然发现这个问题之前,我真的很挣扎,因为我没有使用域上下文,所以我不能使用 Kyle Mcllelens ComboBoxex,也不能使用绝大多数。我确实尝试在我的视图中创建 ViewModel 的实例作为本地资源,但是 a) 我觉得 MVVM 很脏,b) 它无法创建新的 VM 实例,因为我的 VM 需要一个 谢谢
在任何情况下,我只是想知道是否应该有一种方法可以实现更高的性能、更多的 MVVM、更可维护、更好的等等。
,
斯科特
I'm using the datacontextproxy class defined by Dan Wahlin to solve what seemed so very difficult for the many many hours leading up to that discovery.
I have a combobox that is bound to an ObservableCollection<Accounts>
. The combobox also has the values: DisplayMemberPath=AccountNumber
, and the SelectedValue={Binding SelectedAccount}
. I then have a DataForm that has its CurrentItem property also bound to SelectedAccount. Inside my DataForm, I have a handful of fields that require additional dropdowns (AcctCode, UsageCode, etc). To mitigate the binding issues I ran into inside of a DataForm, I implemented the datacontextproxy, allowing me to set the ItemsSource of my cbobox to {Binding Source={StaticResource DataContextProxy}, Path=DataSource.AccountCodes}"
. This is working great in terms of the end result.
Before stumbling onto this, I really struggled with this find. I'm not using a domaincontext, so I couldn't use Kyle Mcllelans ComboBoxex, nor could I use the vast majority of solutions offered. I did try to create an instance of my ViewModel as a local resource in my View, but a) I felt dirty in terms of MVVM, and b) It failed to create a new VM instance, as my VM requires a number of service references to be passed in on construct.
In any case, I'm simply wondering if there's a way I should be doing this that's either more performant, more MVVM, more maintainable, more bestest'er, etc etc.
Thanks,
Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Scott 我也决定使用 DataContextProxy。我认为使用它仍然提供了一个很好的 MVVM 解决方案,其中视图仅通过绑定或命令了解 VM。
我认为在 Silverlight 5 发布之前将其用作临时解决方案。 Silverlight 5 中引入了祖先绑定,这将为数据绑定提供更大的灵活性。
Scott I also have settled on using the DataContextProxy. I think use of it still provides a good MVVM solution where the view only has knowledge of the VM via binding or commanding.
I see use of it as a temporary solution until Silverlight 5 ships. In Silverlight 5 ancestor binding is introduced which will provide more flexibility in data binding.