There is a certain collection ICollection<OperandItemVM> Operands

OperandItemVM looks like this:

 public class OperandItemVM : INotifyPropertyChanged { public string OperandName { get; set; } public string Value{ get; set; } public string Path{ get; set; } } 

It is impossible to display this information in this form (single-level tree): enter image description here

I am trying this way right now, but the disclosure icon is not displayed:

 <TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Operands}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type vm:OperandItemVM}"> <StackPanel Orientation="Horizontal" MinHeight="25"> <TextBox Text="{Binding OperandName}" VerticalAlignment="Center"/> <Button Width="19" Height="19" Margin="15,0,0,0"> <TextBlock Text="X" VerticalAlignment="Center"/> </Button> </StackPanel> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate DataType="{x:Type vm:OperandItemVM}"> <Grid Margin="3"> <Grid.RowDefinitions> <RowDefinition Height="Auto" MinHeight="23"/> <RowDefinition Height="3"/> <RowDefinition Height="Auto" MinHeight="23"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" MinWidth="91"/> <ColumnDefinition Width="3"/> <ColumnDefinition Width="3*" MinWidth="101"/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Значение"/> <Border Grid.Row="0" Grid.Column="2"> <TextBlock Text="{Binding Value}"/> </Border> <TextBlock Grid.Row="2" Grid.Column="0" Text="Путь"/> <Border Grid.Row="0" Grid.Column="2"> <TextBlock Text="{Binding Path}"/> </Border> </Grid> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </TreeView.ItemTemplate> 

Please tell me where I am wrong?

    2 answers 2

    If this is a single-level tree, then try using DataTemplate itself instead of HierarchicalDataTemplate. I can not write while in the comments, I will write in the answers

    The answer above is no good

    In your case, it is better to use ListView, TreeView is not suitable for your purposes.

     <ListView ItemsSource={Binding Operands}> <ListView.ItemTemplate> <DataTemplate> <Expander Header="{Binding OperandName}"> <StackPanel> <TextBlock Text="{Binding Value, StringFormat='Значение - {0}'}"/> <TextBlock Text="{Binding Path, StringFormat='Путь - {0}'}"/> </StackPanel> </Expander> </DataTemplate> </ListView.ItemTemplate> </ListView> 
    • and how to override both the content and Header? - alladuh
    • @alladuh, initially did not understand the essence of the question. Fixed the answer - FrozDark
    • TreeView is not good or simply impossible to use? - alladuh
    • one
      @alladuh, you can use it, but it will be about the same as ListView. You can try instead of ListView, substitute TreeView, will look the same. TreeView is still a tree where you can display a lot of branches. In order for your TreeView to work properly, you need to transfer the Value and Path properties to a separate class, add this class in OperandItemVM, then in the HierarchicalDataTemplate specify the same class in the ItemsSource property and set the design in the ItemTemplate. In the comments, it is not convenient to describe the code, so if necessary, I can attach with another answer what I meant in more detail - FrozDark
    • @alladuh, explain why you need a TreeView? - FrozDark

    FrozDark gave a useful answer, but still I first looked for a solution for TreeView. It turned out a little crutch, IMHO, but so far it has not failed.

    In the ViewModel I created a collection that stores only one object (the ViewModel itself).

    I will explain: there is a UserControl , which has two HierarchicalDataTemplate defined in the resources

     <UserControl x:Class="ExpressionsEditor.View.OperandsList" 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:local="clr-namespace:ExpressionsEditor" xmlns:vm="clr-namespace:ExpressionsEditor.ViewModel" xmlns:model="clr-namespace:ExpressionsEditor.Model" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:view="clr-namespace:ExpressionsEditor.View" xmlns:conv="clr-namespace:ExpressionsEditor.Converters" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <ResourceDictionary> <conv:IsNodeConverter x:Key="IsNodeConverter"/> <HierarchicalDataTemplate x:Key="SecondLevel" DataType="{x:Type model:OperandItemModel}"> <StackPanel Orientation="Horizontal" Margin="0,5,0,5"> <view:PSRMeas/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key="FirstLevel" DataType="{x:Type model:OperandItemModel}" ItemTemplate="{StaticResource SecondLevel}" ItemsSource="{Binding CurrentItem}" > <StackPanel Orientation="Horizontal" Margin="0,5,0,5" VerticalAlignment="Center"> <TextBox Text="{Binding OperandName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="tb_TextChanged" VerticalAlignment="Center"/> <Button Width="21" Height="21" Margin="5,0,0,0"> <TextBlock Text="X" VerticalAlignment="Center"/> </Button> </StackPanel> </HierarchicalDataTemplate> </ResourceDictionary> </UserControl.Resources> <TreeView ItemTemplate="{StaticResource ResourceKey=FirstLevel}" ItemsSource="{Binding Operands}" x:Name="operandsList" Grid.Column="1" BorderThickness="0" Margin="0,5,0,0"> </TreeView> </UserControl> 

    In the first level, I pass the collection of the form:

     public ICollection<OperandItemModel> Operands 

    And OperandItemModel contains the public ObservableCollection<OperandItemModel> CurrentItem , which stores a single OperandItemModel object. This collection is passed to the second level. So Achieved the desired result:

    enter image description here