带有视图模型的 Xaml 动态网格
您好,我正在尝试在 xmal 中创建一个通过 ViewModel 填充的网格。 网格是 5x5 网格,我的 ViewModel 包含“MyObject”列表。该对象包含 2 个 int 变量 Row 和 Column,它们指示对象应位于网格中的位置。 MyObject 也是一个视图模型,网格空间应填充数据模板名称 MyTemplate,并将 MyObject 作为 DataContext。 现在我对 xaml 和视图模型还很陌生,但是最好的方法是什么?
Hello i am trying to create a grid in xmal that is populated via a ViewModel.
The grid is a 5x5 grid, and my ViewModel contains a list of "MyObject". This object contains 2 int variables Row and Column witch indicates where in the grid the Object should be.
MyObject is also a view models, and the grid space should be filled with Data Template names MyTemplate with the MyObject as the DataContext.
Now I pretty new to xaml and view models, but how would the best way of doing the be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们看看...
将 ObservableCollection 放入 VM,然后...
选项 1:
...一个简单的 Grid 到 V 中。从 xaml.cs 订阅它的 CollectionChanged 事件,为每个添加的对象添加 ContentControls 到网格,将 ContentControl 的 Grid.Row 和 Grid.Column 属性绑定到每个对象的属性,并将 DataContext 设置为对象本身和 ContentTemplate 到 Resources["MyTemplate"]。 (此外,对于任何删除的对象,找到相应的 ContentControl 并将其删除。)
优点:简单
缺点:.cs,没有设计器支持
选项 2:
...将 ItemsControl 放入 V 中。将其 ItemPanelTemplate 设置为 Grid,并将 ObservableCollection 绑定到其 ItemsSource。然后将您的 Itemtemplate 设置为 MyTemplate。现在,技巧是根据您的值将项目放入正确的单元格中。为此,请使用 ItemsControl 的 ItemContainerStyle 属性,并将容器的 Grid.Row 和 Column 绑定到这些属性。
优点:漂亮的 xaml 且易于扩展
缺点:ItemContainerStyle 在 WPF 中很棘手,而在 Silverlight 中缺失,因此如果是后者,您可以忘记它
选项 3:
...您编写的自定义面板。也许从 Grid 继承它。如果将一个元素放入其中,请检查它的 DataContext 是否实现了包含您的属性的接口。然后将其用作选项 2 中的 ItemsPanel...
优点:它应该可以完美工作
缺点:您必须为此编写一个新面板...
希望这 3 个面板中的一个适合您。
Let's see...
Put an ObservableCollection into the VM, then...
Option 1:
...a simple Grid into V. Subscribe to it's CollectionChanged event from xaml.cs, add ContentControls for each Added object to grid, bind the ContentControl's Grid.Row and Grid.Column property to each object's properties, and set the DataContext to the object itself and ContentTemplate to Resources["MyTemplate"]. (Also, for any removed objects find the corresponding ContentControl and remove it.)
Pro: easy
Con: .cs, no designer support
Option 2:
...an ItemsControl into V. Set its ItemPanelTemplate to a Grid, and bind your ObservableCollection to its ItemsSource. Then set your Itemtemplate to MyTemplate. Now, the trick is to make the items put in the correct cell, based on your values. For that, use the ItemsControl's ItemContainerStyle property, and bind the container's Grid.Row and Column to those properties.
Pro: nice xaml and easily extended
Con: ItemContainerStyle is tricky in WPF and missing in Silverlight, so in case of the latter, you can forget it
Option 3:
...a custom panel which you write. Inherit it from Grid perhaps. Than if an element is put into it, check if it's DataContext implements an interface which contains your properties. Then use this as an ItemsPanel in Option 2...
Pro: it should work perfectly
Con: You have to write a new Panel for this...
Hopefully one of these 3 will be ok for you.