开发者

WPF : How to create separate Resources for each item in a bound ItemsControl

开发者 https://www.devze.com 2023-02-01 00:56 出处:网络
I want to achieve the following: My ViewModel exposes a property named \'Categories\' which is a collection of CategoryViewModel objects

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 C开发者_运维技巧ategories}">
        <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.


OK, this is a bit of a long-shot, but could you use the DataGrid.Tag ...

<TabControl.ContentTemplate>
    <DataTemplate>
        <xcdg:DataGridControl
                ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"
                AutoCreateColumns="True">
            <xcdg:DataGridControl.Tag>
                 <xcdg:DataGridCollectionViewSource x:Key="GridData"
                      Source="{Binding Items}" />
            </xcdg:DataGridControl.Tag>
        </xcdg:DataGridControl>
    </DataTemplate>
</TabControl.ContentTemplate>

Or ... resources can be defined on any FrameworkElement, so you could try:

<TabControl.ContentTemplate>
    <DataTemplate>
        <xcdg:DataGridControl
                ItemsSource="{Binding Source={StaticResource GridData}}"
                AutoCreateColumns="True">
            <xcdg:DataGridControl.Resources>
                 <xcdg:DataGridCollectionViewSource x:Key="GridData"
                      Source="{Binding Items}" />
            </xcdg:DataGridControl.Resources>
        </xcdg:DataGridControl>
    </DataTemplate>
</TabControl.ContentTemplate>

I don't use the eXceed Grid so cannot test whether these work - just a couple of ideas to try!

Colin E.


You can use x:Shared="True" attribute on a resource. That means a new instance is created for every use of that resource.

Example:

<UserControl.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="GridData" 
        x:Shared="False"
        Source="{Binding Items}" />
</UserControl.Resources>
0

精彩评论

暂无评论...
验证码 换一张
取 消