WPF Treeview with Combox does not work

Lloyd Sheen 1,476 Reputation points
2020-10-24T22:47:20.497+00:00

I have now tried about everything. I have the following:

            <HierarchicalDataTemplate DataType="{x:Type local:SeasonList}">
                    <StackPanel Orientation="Horizontal">
                        <!--<TextBlock Text="{Binding}" FontSize="16" FontWeight="Bold"></TextBlock>-->
                        <TextBlock Text="Season: " FontSize="16" FontWeight="Bold"></TextBlock>
                    <ComboBox ItemsSource="{Binding Season}" SelectedItem="{Binding selectedSeason}" SelectedIndex="{Binding selectedIndex}" SelectionChanged="ComboBox_SelectionChanged" Width="100"></ComboBox>
                    </StackPanel>
            </HierarchicalDataTemplate>

Now when I run the SelectedItem get gets called once. The same for the SelectedIndex as the get gets called (before I do anything. Once the GUI is shown I then select an item from the combobox. Nothing gets called including the SelectionChanged event which is called once before the GUI is set. Every other control in the treeview acts as expected. I have read bit about this and nothing they say seems to work.

Without the events/property changes being called this becomes useless.

HELP???

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,756 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-10-25T14:22:24.387+00:00

    Hi Lloyd,
    your code works fine. Try following demo:

    XAML MainWindow

    <Window x:Class="WpfApp1.Window009"  
            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:WpfApp009"  
            mc:Ignorable="d"  
            Title="Window009" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <Grid.RowDefinitions>  
          <RowDefinition/>  
          <RowDefinition/>  
        </Grid.RowDefinitions>  
        <TreeView ItemsSource="{Binding View}">  
          <TreeView.Resources>  
            <HierarchicalDataTemplate DataType="{x:Type local:SeasonList}">  
              <StackPanel Orientation="Horizontal">  
                <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold" Margin="5"/>  
                <TextBlock Text="Season: " FontSize="16" FontWeight="Bold" Margin="5"/>  
                <ComboBox ItemsSource="{Binding Season}"   
                          SelectedItem="{Binding selectedSeason}"   
                          SelectedIndex="{Binding selectedIndex}"   
                          Width="100"/>  
              </StackPanel>  
            </HierarchicalDataTemplate>  
          </TreeView.Resources>  
        </TreeView>  
        <DataGrid Grid.Row="1" ItemsSource="{Binding View}" AutoGenerateColumns="False">  
          <DataGrid.Columns>  
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>  
            <DataGridTextColumn Header="selectedSeason" Binding="{Binding selectedSeason}"/>  
            <DataGridTextColumn Header="selectedIndex" Binding="{Binding selectedIndex}"/>  
          </DataGrid.Columns>  
        </DataGrid>  
      </Grid>  
    </Window>  
    

    And classes:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace WpfApp009  
    {  
      public class ViewModel  
      {  
        public ViewModel()  
        {  
          for (int i = 1; i < 10; i++)  
          {  
            col.Add(new SeasonList()  
            {  
              Name = $"Node {i}",  
              Season =  
              new ObservableCollection<string>(new string[] { "Spring", "Summer", "Autumn", "Winter" })  
            });  
          }  
          cvs.Source = col;  
        }  
      
        public ICollectionView View { get => cvs.View; }  
        private CollectionViewSource cvs = new CollectionViewSource();  
        private ObservableCollection<SeasonList> col = new ObservableCollection<SeasonList>();  
      }  
      
      public class SeasonList  
      {  
        public string Name { get; set; }  
        public ObservableCollection<string> Season { get; set; } = new ObservableCollection<string>();  
        public string selectedSeason { get; set; }  
        public int selectedIndex { get; set; }  
      }  
    }  
    

    Result:

    34738-x.gif

    0 comments No comments

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.