WPF:如何为绑定 ItemsControl 中的每个项目创建单独的资源
我想实现以下目标:
- 我的 ViewModel 公开一个名为“Categories”的属性,它是 CategoryViewModel 对象的集合
- 每个 CategoryViewModel 对象都公开一个名为“Items”的属性,它是字符串*的集合。
- 在我的视图中,我想要一个 TabControl,其中“类别”集合中的每个对象都有一个 TabItem。
每个 TabItem 的内容应该是一个 xceed DataGrid 控件,显示所选选项卡的 Items 集合的内容。
<数据模板> <数据模板>
当我直接绑定到 DataGridControl 的 ItemsSource 属性时,这可以正常工作。但是,为了利用 DataGridControl 的所有功能,我需要将 DataGridControl 的 ItemsSource 属性绑定到绑定到我的 Items 集合的 DataGridCollectionViewSource 对象。当网格未嵌套在另一个控件中时,我通过在 UserControl 的资源部分中创建 DataGridCollectionViewSource 对象并绑定到该对象来执行此操作。
<UserControl>
<UserControl.Resources>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
Source="{Binding Items}" />
</UserControl.Resources>
<Grid>
<xcdg:DataGridControl
ItemsSource="{Binding Source={StaticResource GridData}}"
AutoCreateColumns="True">
</xcdg:DataGridControl>
</Grid>
</UserControl>
我需要如何构造 XAML,以便在绑定 TabControl 时,为每个 TabItem 创建一个 DataGridCollectionViewSource 对象,以便可以将 TabItem 内容中生成的 DataGridControl 绑定到它?
清澈如泥,对吧? :)
谢谢!
注意:
*在实际的解决方案中,集合包含比简单字符串更复杂的类的对象,但使用字符串是为了使示例更简单。
I want to achieve the following:
- My ViewModel exposes a property named 'Categories' which is a collection of CategoryViewModel objects
- Each CategoryViewModel object exposes a property called 'Items' which is a collection of strings*.
- On my View, I want a TabControl with a TabItem for each object in the 'Categories' collection.
The Content of each TabItem should be a xceed DataGrid control displaying the contents of the selected tab's Items collection.
<TabControl ItemsSource="{Binding Categories}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding CategoryName}" /> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate> <xcdg:DataGridControl ItemsSource="{Binding Items}" AutoCreateColumns="True"> </xcdg:DataGridControl> </DataTemplate> </TabControl.ContentTemplate> </TabControl>
This works ok when I bind directly to the ItemsSource property of the DataGridControl. However, in order to utilize all of the functionality of the DataGridControl, I need to bind the ItemsSource property of the DataGridControl to a DataGridCollectionViewSource object that is bound to my Items collection. I do this when the grid ISN'T nested in another control by creating a DataGridCollectionViewSource object in the Resources section of the UserControl and binding to that.
<UserControl>
<UserControl.Resources>
<xcdg:DataGridCollectionViewSource x:Key="GridData"
Source="{Binding Items}" />
</UserControl.Resources>
<Grid>
<xcdg:DataGridControl
ItemsSource="{Binding Source={StaticResource GridData}}"
AutoCreateColumns="True">
</xcdg:DataGridControl>
</Grid>
</UserControl>
How do I need to structure the XAML so that when the TabControl is being bound, a DataGridCollectionViewSource object is created for each TabItem so that the DataGridControl that is generated within the content of the TabItem can be bound to it?
Clear as mud, right? :)
Thanks!
Notes:
*In the real solution the collection contains objects of a class that is more complex than a simple string, but a string was used to make the example more simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这有点遥远,但是您可以使用 DataGrid.Tag ...
或者...资源可以在任何 FrameworkElement 上定义,所以您可以尝试:
我不使用 eXceed Grid 所以不能测试这些是否有效——只需尝试几个想法!
科林·E。
OK, this is a bit of a long-shot, but could you use the DataGrid.Tag ...
Or ... resources can be defined on any FrameworkElement, so you could try:
I don't use the eXceed Grid so cannot test whether these work - just a couple of ideas to try!
Colin E.
您可以使用
x:Shared="True"
< /a> 资源的属性。这意味着每次使用该资源都会创建一个新实例。例子:
You can use
x:Shared="True"
attribute on a resource. That means a new instance is created for every use of that resource.Example: