Deploy WPF tree with different parent nodes as well as different child nodes?

I want to implement a tree structure with the following structure .....

[RootNode] <---- Tree root
  - [ParentNode P1] <---- ModelClass P1 object
      ---- [ChildNode C1] <----- ModelClass C1 object (also have children of another type)
      - - [ChildNode C2] <----- ModelClass C2 object (there are also children of a different type)
      ---- [ChildNode C3] <----- ModelClass C3 object (also there are children of a different type)
  - [ParentNode Q1 ] <---- ModelClass Q1 object
      ---- [ChildNode B1] <----- ModelClass B1 object (there are also children of a different type)
      ---- [ChildNode B2] <----- ModelClass B2 object (there are also children of another type)
      ---- [ChildNode B3] <----- ModelClass B3 object (there are also children of another type)
  - [ParentNode R1] <---- ModelClass R1 object
      ---- [ChildNode A1] <----- ModelClass A1 object (also have children of another type)
      ---- [ChildNode A2] <----- ModelClass A2 object (also have children of another type)
      ---- [ChildNode A3] <----- ModelClass A3 object (also have children of a different type)

I reviewed many of the proposed solutions on this site, as well as on the Internet ..... but I just can’t figure out how to do this .....

This is my first attempt at Wpf, and this is a key requirement ...

It is also difficult to find an object model for the above classes .....

In all the above classes, there are other properties, including their child nodes ... I do not want to display all properties only child nodes

Completely puzzled ... seeing another solution

It would be great if I could get some help in this regard ...

thank

+5
3

, .... :

public class EntityBase :ObservableCollection<object>
{

}

public class Parent : EntityBase
{

}  

public class ChildA : EntityBase // Dont make it a collection if it has noe childern to be displayed so dont inherit for EntityBase
{
   //Child Properties
}


public class ChildB : EntityBase
{
//Child Properties
}  

, TreeView, ChildA ChildB Parent i.e

    public ObservableCollection<object> GetData()
    {
         var temp = new ObservableCollection<object>();
         Parent parent = new Parent(); // Root Node
         temp.Add(parent);
         parent.Add(new ChildA()); // ChildA as Child1 of Parent

         parent.Add(new ChildA()); // ChildA as Child2 of Parent

         parent.Add(new ChildB()); // ChildB as Child3 of Parent

         parent.Add(new ChildB()); // ChildB as Child4 of Parent

         return temp;

    }  

.

<TreeView Name="test" Grid.Row="0" ItemsSource="{Binding Path=TreeData,Source={StaticResource ResourceKey=DataSource}}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type EntityLayer:Parent}" ItemsSource="{Binding}">
                <StackPanel>
                    <TextBlock>Parent</TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type EntityLayer:ChildA}" ItemsSource="{Binding}">
                <StackPanel>
                    <TextBlock Text="{Binding Path = Name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type EntityLayer:ChildB}" ItemsSource="{Binding}">
                <StackPanel>
                    <TextBlock Text="{Binding Path = Name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
+8

, - , , , DataTemplate .

, , DataTemplateSelector, .

, . List<object> , , datatemplates.

public class P1 : List<object> {
    public P1() {}
    public P1( IEnumerable<object> collection ) : base( collection ) {}
}

, List<object>, .

Window:

public MainWindow() {
    InitializeComponent();
    this.DataContext = MyDataSource.GetData(); // in which I construct the tree of parents and children
}

HierarchicalDataTemplate . - , , , DataTemplate :

<HierarchicalDataTemplate DataType="{x:Type loc:P1}"
                          ItemsSource="{Binding}">
    <TextBlock>a P1 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:C1}"
                          ItemsSource="{Binding}">
    <TextBlock>a C1 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:C2}"
                          ItemsSource="{Binding}">
    <TextBlock>a C2 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:Q1}"
                          ItemsSource="{Binding}">
    <TextBlock>a Q1 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:B1}"
                          ItemsSource="{Binding}">
    <TextBlock>a B1 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:B2}"
                          ItemsSource="{Binding}">
    <TextBlock>a B2 object</TextBlock>
</HierarchicalDataTemplate>

List<object>, , , ItemsSource="{Binding}". Children, , , ItemsSource="{Binding Children}". .

DataTemplateSelector - . DataType, x:Key , (List<object>) WPF , , P1/Q1/ .. HierarchicalDataTemplate . TreeView :

<TreeView ItemsSource"{Binding}" />

, , , DataTemplateSelector, , Type. x:Key datatemplates, :

<HierarchicalDataTemplate DataType="{x:Type loc:P1}" x:Key="myKeyforP1"
                          ItemsSource="{Binding}">
    <TextBlock>a P1 object</TextBlock>
</HierarchicalDataTemplate>

Dictionary, , Type ( , ):

public class CustomDataTemplateSelector : DataTemplateSelector {
    static Dictionary<Type, object> typeToKey = new Dictionary<Type, object>();
    static CustomDataTemplateSelector() {
        typeToKey[ typeof( P1 ) ] = "myKeyforP1";
    }

    public override DataTemplate SelectTemplate( object item, DependencyObject container ) {
        var element = container as FrameworkElement;
        if ( element != null && item != null ) {
            var itemtype = item.GetType();
            object keyObject;
            if ( typeToKey.TryGetValue( itemtype, out keyObject ) ) {
                var template = element.TryFindResource( keyObject ) as DataTemplate;
                if ( template != null ) {
                    return template;
                }
            }
        }
        return base.SelectTemplate( item, container );
    }
}

, TreeView :

<Grid.Resources>
    <loc:CustomDataTemplateSelector x:Key="mySelector" />
</Grid.Resources>
<TreeView ItemsSource="{Binding}"
          ItemTemplateSelector="{StaticResource mySelector}"></TreeView>

base.SelectTemplate() Type , HierarchicalDataTemplate , , TreeView custom DataTemplateSelector:

<HierarchicalDataTemplate DataType="{x:Type loc:P1}"
                          ItemsSource="{Binding}">
    <TextBlock>a P1 object</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type loc:P1}" x:Key="myKeyforP1"
                          ItemsSource="{Binding}">
    <TextBlock>a P1 that looks much different</TextBlock>
</HierarchicalDataTemplate>
+2

. , , .

Public string Name { get; set; }
Public ObservableCollection<ChildA> ChildrenA { get; set; }
Public ObservableCollection<ChildB> ChildrenB { get; set; }

. , , - ChildA ChildB.

0

All Articles