Adding items at runtime

Apr 3, 2009 at 6:41 AM
Adding items to the collection at runtime does not update the treeview even though i am using observable collections. Any workaround for this?

thanks
Coordinator
Apr 3, 2009 at 7:41 PM
Where are the observable collections you are modifying?  Are they top-most collections or collections in a class that is a node of the tree?  Could you post some code?
Aug 6, 2009 at 3:33 PM

Hi,

I modified the sample. Now all classes (ClassA, ClassB and ClassC) use ObservableCollection<T> instead of List<T>. I added a new item to classA1.ListOfClassC at runtime, and the tree was updated correctly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace DaisleyHarrison.WPF.ComplexDataTemplates.UnitTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            this.Closed += new EventHandler(Window1_Closed);
            InitializeComponent();

            List<ClassA> listOfClassA = new List<ClassA>();
            ClassA classA1 = new ClassA();
            classA1.Label = "Class A-1";
            classA1.ToolTip = "This is Class A-1";
            ClassB classB11 = new ClassB();
            classB11.Label = "Class B-1-1";
            classB11.ToolTip = "This is Class B-1-1";
            classA1.ListOfClassB.Add(classB11);
            ClassB classB12 = new ClassB();
            classB12.Label = "Class B-1-2";
            classB12.ToolTip = "This is Class B-1-2";
            classA1.ListOfClassB.Add(classB12);
            ClassC classC11 = new ClassC();
            classC11.Label = "Class C-1-1";
            classC11.ToolTip = "This is Class C-1-1";
            classA1.ListOfClassC.Add(classC11);
            ClassC classC12 = new ClassC();
            classC12.Label = "Class C-1-2";
            classC12.ToolTip = "This is Class C-1-2";
            Item item121 = new Item();
            item121.Label = "Rocky Road";
            item121.ToolTip = "Rocky Road";
            classC12.IceCream.Add(item121);
            Item item122 = new Item();
            item122.Label = "Chocolate";
            item122.ToolTip = "Chocolate";
            classC12.IceCream.Add(item122);
            classA1.ListOfClassC.Add(classC12);
            listOfClassA.Add(classA1);

            ClassA classA2 = new ClassA();
            classA2.Label = "Class A-2";
            classA2.ToolTip = "This is Class A-2";
            ClassB classB21 = new ClassB();
            classB21.Label = "Class B-2-1";
            classB21.ToolTip = "This is Class B-2-1";
            classA2.ListOfClassB.Add(classB21);
            ClassB classB22 = new ClassB();
            classB22.Label = "Class B-2-2";
            classB22.ToolTip = "This is Class B-2-2";
            classA2.ListOfClassB.Add(classB22);
            ClassC classC21 = new ClassC();
            classC21.Label = "Class C-2-1";
            classC21.ToolTip = "This is Class C-2-1";
            Item item211 = new Item();
            item211.Label = "Very Expensive Dill";
            item211.ToolTip = "Very Expensive Green Dill Pickles";
            classC21.Pickles.Add(item211);
            Item item212 = new Item();
            item212.Label = "Very Expensive Bread and Butter";
            item212.ToolTip = "Very Expensive Sweat Bread and Butter Pickles";
            classC21.Pickles.Add(item212);
            classA2.ListOfClassC.Add(classC21);
            ClassC classC22 = new ClassC();
            classC22.Label = "Class C-2-2";
            classC22.ToolTip = "This is Class C-2-2";
            Item item221 = new Item();
            item221.Label = "Deluxe Rocky Road";
            item221.ToolTip = "RDeluxe ocky Road";
            classC22.IceCream.Add(item221);
            Item item222 = new Item();
            item222.Label = "Deluxe Chocolate";
            item222.ToolTip = "Deluxe Chocolate";
            classC22.IceCream.Add(item222);
            Item item223 = new Item();
            item223.Label = "Deluxe Dill";
            item223.ToolTip = "Deluxe Green Dill Pickles";
            classC22.Pickles.Add(item223);
            Item item224 = new Item();
            item224.Label = "Deluxe Bread and Butter";
            item224.ToolTip = "Deluxe Sweat Bread and Butter Pickles";
            classC22.Pickles.Add(item224);
            classA2.ListOfClassC.Add(classC22);
            listOfClassA.Add(classA2);

            this.textTreeView.ItemsSource = listOfClassA;

			_classA1 = classA1;
        }

		private readonly ClassA _classA1;

        void Window1_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			_classA1.ListOfClassC.Add(new ClassC() { Label = "I am NEW" });
		}
    }
}

 

<Window x:Class="DaisleyHarrison.WPF.ComplexDataTemplates.UnitTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DaisleyHarrison.WPF.ComplexDataTemplates.UnitTest"
    xmlns:complex="clr-namespace:DaisleyHarrison.WPF.ComplexDataTemplates;assembly=ComplexDataTemplates"
    Title="ComplexDataTemplates UnitTest" Height="506" Width="389">
    
	<Window.Resources>
        <complex:ComplexGroupDataTemplateSelector x:Key="complex-group-data-template-selector"/>
        <complex:ComplexGroupConverter x:Key="complex-group-converter"/>
        <HierarchicalDataTemplate DataType="{x:Type local:ClassA}">
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource complex-group-converter}">
                    <Binding Path="ListOfClassB"/>
                    <Binding Path="ListOfClassC"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/ClassA.png" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}" VerticalAlignment="Center"  Margin="5,0,0,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IEnumerable[ClassB]" ItemsSource="{Binding Path=.}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classBs.png" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock VerticalAlignment="Center"  Margin="5,0,0,0">List of Class Bs</TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IEnumerable[ClassC]" ItemsSource="{Binding Path=.}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classCs.png" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock VerticalAlignment="Center"  Margin="5,0,0,0">List of Class Cs</TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IEnumerable[Item]" ItemsSource="{Binding Path=.}">
            <TextBlock>List of Items</TextBlock>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="Pickles-Template" ItemsSource="{Binding Path=.}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/pickle.png" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock VerticalAlignment="Center"  Margin="5,0,0,0">Pickles</TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IceCream-Template" ItemsSource="{Binding Path=.}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/icecream.png" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock VerticalAlignment="Center"  Margin="5,0,0,0">Ice Cream</TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:ClassB}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classB.png" VerticalAlignment="Center" Margin="0,5,0,5"/>
                <TextBlock  VerticalAlignment="Center"  Margin="5,0,0,0">Class B:</TextBlock>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}" VerticalAlignment="Center"  Margin="5,0,0,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:ClassC}">
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource complex-group-converter}" ConverterParameter="Pickles-Template,IceCream-Template">
                    <Binding Path="Pickles"/>
                    <Binding Path="IceCream"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classC.png" VerticalAlignment="Center" Margin="0,5,0,5"/>
                <TextBlock  VerticalAlignment="Center"  Margin="5,0,0,0">Class C:</TextBlock>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}"  VerticalAlignment="Center"  Margin="5,0,0,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:IItem}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/item.png" VerticalAlignment="Center" Margin="0,5,0,5"/>
                <TextBlock VerticalAlignment="Center" Margin="5,0,0,0">Item:</TextBlock>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}" VerticalAlignment="Center" Margin="5,0,0,0"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
	
    <DockPanel LastChildFill="True">
		<Button Content="Add New" Click="Button_Click" DockPanel.Dock="Top" />
		<TreeView x:Name="textTreeView" ItemTemplateSelector="{StaticResource complex-group-data-template-selector}" />
	</DockPanel>
</Window>

<font size="2">

 

</font>