-->

Using CollectionViewSource in an ItemsControl nest

2019-08-16 09:37发布

问题:

I'm trying to use a CollectionViewSource in an ItemsControl that is nested inside another ItemsControl. I have no XAML warnings/errors, but the data is not displayed. If I bind the ItemsSource property directly to the ObservableCollection, there is no problem displaying the items.

My ViewModel is basically a collection nested inside another collection.

XAML

<UserControl x:Class="View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:v="clr-namespace:ViewModel"
             xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <TabControl ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedCategory, Mode=TwoWay}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="v:CategoryViewModel">
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabControl.ContentTemplate>
            <DataTemplate DataType="v:CategoryViewModel">
                <ScrollViewer>
                    <ItemsControl ItemsSource="{Binding Groups}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <DataTemplate.Resources>
                                    <CollectionViewSource x:Key="ValuesCollection" Source="{Binding Values}">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="Name" />
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </DataTemplate.Resources>
                                <GroupBox Header="{Binding Name}" >
                                    <ItemsControl ItemsSource="{Binding Source={StaticResource ValuesCollection}}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid Margin="10,2,10,2">
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition/>
                                                        <RowDefinition/>
                                                    </Grid.RowDefinitions>
                                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"/>
                                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Value}""/>
                                                </Grid>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </GroupBox>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</UserControl>

CategoryViewModel

namespace ViewModel
{
    using System.Collections.ObjectModel;
    using System.Diagnostics;

    internal class CategoryViewModel : ObservableObjectBase
    {
        private string name;
        private string id;
        private int priority;

        public CategoryViewModel()
        {
            this.Groups = new ObservableCollection<GroupViewModel>();
        }

        public ObservableCollection<GroupViewModel> Groups { get; }

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.SetValue(ref this.name, value);
            }
        }

        public string Id
        {
            get
            {
                return this.id;
            }

            set
            {
                this.SetValue(ref this.id, value);
            }
        }

        public int Priority
        {
            get
            {
                return this.priority;
            }

            set
            {
                this.SetValue(ref this.priority, value);
            }
        }
    }
}

GroupViewModel

namespace ViewModel
{
    using System.Collections.ObjectModel;
    using System.Diagnostics;

    internal class GroupViewModel : ObservableObjectBase
    {
        private string name;
        private string id;
        private int priority;

        public GroupViewModel()
        {
            this.Values = new ObservableCollection<ValueViewModel>();
        }

        public ObservableCollection<ValueViewModel> Values { get; }

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.SetValue(ref this.name, value);
            }
        }

        public string Id
        {
            get
            {
                return this.id;
            }

            set
            {
                this.SetValue(ref this.id, value);
            }
        }

        public int Priority
        {
            get
            {
                return this.priority;
            }

            set
            {
                this.SetValue(ref this.priority, value);
            }
        }
    }
}

ValueViewModel

namespace ViewModel
{
    using System.Diagnostics;

    internal class ValueViewModel : ObservableObjectBase
    {
        private string name;
        private string id;
        private string value;
        private int priority;

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.SetValue(ref this.name, value);
            }
        }

        public string Id
        {
            get
            {
                return this.id;
            }

            set
            {
                this.SetValue(ref this.id, value);
            }
        }

        public string Value
        {
            get
            {
                return this.value;
            }

            set
            {
                this.SetValue(ref this.value, value);
            }
        }

        public int Priority
        {
            get
            {
                return this.priority;
            }

            set
            {
                this.SetValue(ref this.priority, value);
            }
        }
    }
}

Any suggestion?

Thanks in advance.

Edit: In the output I see:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Values; DataItem=null; target element is 'CollectionViewSource' (HashCode=40230093); target property is 'Source' (type 'Object')

回答1:

Instead of placing the CollectionViewSource in DataTemplate.Resources, you should place it in the resources of a framework element inside the datatemplate, for example in <GroupBox.Resources>

See related: https://stackoverflow.com/a/6614745/5265292