繁体   English   中英

在ItemsControl中嵌套的ItemsControl中使用CollectionViewSource

[英]Using CollectionViewSource in an ItemsControl nested inside an ItemsControl

我正在尝试在另一个ItemsControl嵌套的ItemsControl中使用CollectionViewSource 我没有XAML警告/错误,但未显示数据。 如果我将ItemsSource属性直接绑定到ObservableCollection ,则显示项目没有问题。

我的ViewModel本质上是一个嵌套在另一个集合中的集合。

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);
            }
        }
    }
}

有什么建议吗?

提前致谢。

编辑:在输出中,我看到:

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path =值; DataItem = null; 目标元素是'CollectionViewSource'(HashCode = 40230093); 目标属性是“源”(类型“对象”)

而不是将CollectionViewSource放置在DataTemplate.Resources ,您应该将其放置在数据模板内部的框架元素的资源中,例如在<GroupBox.Resources>

参见相关文章: https : //stackoverflow.com/a/6614745/5265292

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM