Nested TreeView with advanced properties

This problem has been resolved earlier, but I just do not get it with examples that I find on the Internet.

I have a class, say, 'ClassA', this class has 2 string properties, 'Property1' and 'Property2', as well as IEnumerable, where 'ClassB' also has 2 properties. ClassB list will be displayed in nested treeview

I want them to appear as a tree:

-ClassA [0]
  ClassA.Property1
  ClassA.Property2
  -ClassA.ClassB Title
   ClassB [0]
   ClassB [1]
   Etc.
+ ClassA [1]
+ ClassB [2]

I understand that the way to accomplish this is to use HierarchicalDataTemplates, however all the examples that I can find only tell me how to do this:

-ClassA [0]
  -ClassA.ClassB Title
   ClassB [0]
   ClassB [1]
   Etc.
+ ClassA [1]
+ ClassB [2]

, ClassA . Im , DataTemplate ClassA, - .

.

!

+3
1

, , , .

itemtemplate , itemtemplate .

, , , , HierarchicalDataTemplates.

WPF:

    <TreeView HorizontalAlignment="Left" Name="treeView1" VerticalAlignment="Top">
        <TreeView.ItemTemplate>
            <DataTemplate>
                <TreeViewItem Header="{Binding FileName}">
                    <TextBlock Text="{Binding MetaData1}"/>
                    <TextBlock Text="{Binding MetaData2}"/>
                    <TreeViewItem ItemsSource="{Binding Mappings}" Header="Mappings">
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="17"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Original}" Grid.Column="0"/>
                                    <TextBlock Text="->" Grid.Column="1" Margin="3,0,3,0"/>
                                    <TextBlock Text="{Binding Mapping}" Grid.Column="2"/>
                                </Grid>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                </TreeViewItem>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

:

public class ClassA
{
    public string MetaData1 { get; set; }
    public string MetaData2 { get; set; }
    public string FileName { get; set; }
    public List<ClassB> Mappings { get; set; }
}

public class ClassB
{
    public string Original { get; set; }
    public string Mapping { get; set; }
}

:

new List<ClassA>
                    {
                        new ClassA
                            {
                                FileName = "ClassA 1",
                                MetaData1 = "Prop 1",
                                MetaData2 = "Prop 2",
                                Mappings = new List<ClassB>
                                            {
                                                new ClassB
                                                {
                                                    Original = "BProp 1",
                                                    Mapping = "BProp 2"
                                                }
                                            }
                            },
                        new ClassA
                            {
                                FileName = "ClassA 2",
                                MetaData1 = "Prop 1",
                                MetaData2 = "Prop 2",
                                Mappings = new List<ClassB>
                                            {
                                                new ClassB
                                                {
                                                    Original = "BProp 1",
                                                    Mapping = "BProp 2"
                                                }
                                            }
                            }
                    };

- , ( HierachicalDataTemplates DataTemplates Im, .

+1
source

Source: https://habr.com/ru/post/1763825/


All Articles