WPF工具包数据网格
你好,我正在构建一个带有数据网格的 wpf 应用程序, 该模式是模型视图视图模型。
我的所有屏幕都包含一个内容控件,我只是为他分配了视图模型,该模型具有合适的数据模板,
无论如何,我的问题是组合框列,数据上下文是呈现的实体,我需要它作为视图模型。
最好的解决方案是什么?
hello i'm building a wpf app with data grids,
the pattern is model view view model.
all og my screens contains a contentcontrol, and i just assign him the view model, that have a suitable data template,
anyway, my problem is with combo box column, the data context is the presented entity, and i need it to be the view model.
whats the best solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我正在使用另一个数据网格,但它可能类似。 我这样做的方式是这样的:
在XAML中,我在资源中定义了一个ObjectDataProvider:
然后在分配DataContext(构造函数或DataContextChanged事件)之后,我这样做了:
在Combobox xaml中,我将其用作绑定来源:
不确定它是否适用于微软数据网格,但我想值得一试。
I'm using another datagrid, but it might be similar. The way i did it was like that:
in the XAML, i defined an ObjectDataProvider in the resources:
then after assigning the DataContext (either the constructor or the DataContextChanged event), i did this:
In the Combobox xaml, i used that as binding source:
Not sure if it works for the microsoft datagrid, but i guess it's worth a try.
这就是我将 ViewModel 与 ComboBox 结合使用的方式,DataContext 是 ViewModel,而不是底层实体(List)。
ViewModel(Person 是一个带有名称和年龄的简单类):
ComboBox 的 XAML:
在后面的代码中我可以这样做:
希望这有帮助,我对 WPF 很陌生,如果这是正确的,我想听到任何反馈使用MVVM的方式,我认为它非常优雅,因为你只在代码中处理ViewModel和Model,并且View可以被替换。
this is how I used ViewModel with ComboBoxes, the DataContext is the ViewModel, not the underlying entity (List<Person>).
ViewModel (Person is a Simple class with Name and Age):
XAML for ComboBox:
And in code behind I can do:
Hope this helps, I'm quite new to WPF, I'd like to hear any feedback if this is the right way to use MVVM, I think it's quite elegant since you only deal with the ViewModel and Model in code, and the View can be replaced.
我发现实现此目的的最佳方法是为我在网格中使用的所有查找定义一些外部类,并将它们作为静态资源嵌入到模板中
I Found that the best way of implementing this is define some external class for all lookups that i use in grid and embedd them in the template as a static resource
我们最终为每个组合框列表提供了具有静态属性的类:(
您不能将类本身设置为静态,否则 XAML 将无法打开它,但您也不会得到编译错误)
例如:
然后在 XAML 中:
We ended up having classes with static properties for each of of our combo box lists:
(you can't make the class itself static otherwise XAML won't be able to open it, but you won't get compile errors)
For example:
and then in XAML: