How to create Dynamic wpf treeview

Emre Geçer 26 Reputation points
2022-06-19T13:31:14.617+00:00

212708-wpf.png

I need to create something like this. The buttons has to add new subobject to treeview. How can i do that ?
If anyone can help me i'll be grateful.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,813 questions
0 comments No comments
{count} vote

Accepted answer
  1. Peter Fleischer (former MVP) 19,331 Reputation points
    2022-06-20T05:15:54.22+00:00

    Hi,
    try following demo:

    XAML:

    <Window x:Class="WpfApp1.Window026"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp026"  
            mc:Ignorable="d"  
            Title="EmreGeer-4824_220619" Height="450" Width="400">  
      <Window.Resources>  
        <local:ViewModel x:Key="vm"/>  
      </Window.Resources>  
      <Grid DataContext="{StaticResource vm}">  
        <TreeView ItemsSource="{Binding View}">  
          <TreeView.ItemTemplate>  
            <HierarchicalDataTemplate DataType="{x:Type local:Data}" ItemsSource="{Binding SubItems}">  
              <Grid>  
                <Grid.ColumnDefinitions>  
                  <ColumnDefinition Width="200"/>  
                  <ColumnDefinition />  
                </Grid.ColumnDefinitions>  
                <StackPanel Grid.Column="0">  
                  <TextBlock Text="{Binding Name}"/>  
                  <TextBlock Text="{Binding Description}"/>  
                </StackPanel>  
                <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">  
                  <Button Content="+" Margin="5" Command="{Binding Add, Source={StaticResource vm}}" CommandParameter="{Binding}"/>  
                  <Button Content="X" Margin="5" Command="{Binding Del, Source={StaticResource vm}}" CommandParameter="{Binding}"/>  
                </StackPanel>  
              </Grid>  
            </HierarchicalDataTemplate>  
          </TreeView.ItemTemplate>  
        </TreeView>  
      </Grid>  
    </Window>  
    

    and code see attached file because is forbidden to post this code: 212812-x.txt

    Result:

    212823-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.