Treeview的ItemContainerStyle使用带有SubClasses列表的Class

时间:2017-04-06 05:41:39

标签: c# wpf treeview

我的ObservableCollection来自Type FUClass。在这个类中是一个FUSubClass的列表。 ListView Itemsource绑定到ObservableCollection FUList。

My TreeView显示GroupName,并且有一个TreeViewItem和ItemName。

问题是只显示了一个包含FUSubClassList中所有项目的TreeViewItem,但我希望每个Item都有一个TreeViewItem。 不知道如何在ItemContainerStyle中实现它。

XAML ItemContainerStyle:

<Style PresentationOptions:Freeze="True" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="Auto" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <TreeView Width="1248" MinHeight="50">
                        <TreeViewItem>
                            <TreeViewItem.Header>
                                <StackPanel Orientation="Horizontal">
                                    <ContentPresenter Content="{Binding GroupName}" RecognizesAccessKey="False" />
                                </StackPanel>
                            </TreeViewItem.Header>
                            <TreeViewItem ItemsSource="{Binding FUSubClassList}">
                                <TreeViewItem.Header>
                                    <Grid>
                                        <ContentPresenter Content="{Binding ItemName}" RecognizesAccessKey="False" />
                                    </Grid>
                                </TreeViewItem.Header>
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

C#

    public class FUClass : INotifyPropertyChanged
    {
        private string groupName;
        public string GroupName
        {
            get { return this.groupName; }
            set
            {
                if (this.groupName != value)
                {
                    this.groupName = value;

                    this.OnPropertyChanged(nameof(GroupName));
                }
            }
        }
        private ObservableCollection<FUSubClass> FUSubClassList = new ObservableCollection<FUSubClass>();
        public ObservableCollection<FUSubClass> FUSubClassList
        {
            get { return FUSubClassList; }
            set { FUSubClassList = value; }
        }

        public class FUSubClass : INotifyPropertyChanged
        {   
            private string itemName;
            public string ItemName
            {
                get { return this.itemName; }
                set
                {
                    if (this.itemName != value)
                    {
                        this.itemName = value;

                        this.OnPropertyChanged(nameof(ItemName));
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private ObservableCollection<FUClass> FUList = new ObservableCollection<FUClass>();

UI

**UI**

1 个答案:

答案 0 :(得分:1)

跳过ItemContainerStyle并为您的数据类型定义数据模板,例如:

<TreeView ItemsSource="{Binding FUList}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:FUClass}" ItemsSource="{Binding FUSubClassList}">
            <TextBlock Text="{Binding GroupName}" />
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:FUSubClass}">
            <TextBlock Text="{Binding ItemName}" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

请提供最小,完整且可验证的示例,您需要任何进一步的帮助:https://stackoverflow.com/help/mcve