views:

24

answers:

0

Hello,

I have source-ObservableCollection holding hierarchical data in configuration A, and i want to display it using a treeview in configuration B(add custom parent-nodes, display different types of data on same hierarchy level). i've managed to to it only by setting the OC (ObservableCollection) hierarchy to match the exact desired output hierarchy + the use of style selector.

i want to do it only by using xaml+binding.

here's the xaml:

    <!--selection problem-->
    <DataTemplate x:Key="t1">
        <TreeViewItem Header="{Binding Name}">
            <TreeViewItem.Items >
                <TreeViewItem Header="node1"/>
                <TreeViewItem Header="node2"/>
            </TreeViewItem.Items>
        </TreeViewItem>
    </DataTemplate>

    <!--not displaying & recursion-->
    <HierarchicalDataTemplate x:Key="t2">
        <TextBlock Text="{Binding Name}"/>
        <HierarchicalDataTemplate.ItemsSource >
            <Binding >
                <Binding.Source>
                    <ms:ArrayList>
                        <TextBlock Text="node1"/>
                        <TextBlock Text="node2"/>
                    </ms:ArrayList>
                 </Binding.Source>
            </Binding>
        </HierarchicalDataTemplate.ItemsSource>
    </HierarchicalDataTemplate>

    <!--display only single node - one time-->
    <HierarchicalDataTemplate x:Key="t3" >
        <TextBlock Text="{Binding Name}"/>
        <HierarchicalDataTemplate.ItemsSource>
            <Binding>
                <Binding.Source>
                    <ms:ArrayList>
                        <TreeViewItem Header="Node1" ItemsSource="{Binding OC}" ItemTemplate="{StaticResource Template}"/>
                        <TreeViewItem Header="Node2" >
                            <TreeViewItem.Items>
                                <TreeViewItem Header="{Binding Cont}"/>
                            </TreeViewItem.Items>
                        </TreeViewItem>
                    </ms:ArrayList>
                </Binding.Source>
            </Binding>
        </HierarchicalDataTemplate.ItemsSource>

    </HierarchicalDataTemplate>


</Window.Resources>
<Grid>
    <TreeView x:Name="tv" ItemTemplate="{StaticResource  t3}" Background="Pink" />
</Grid>

code behind:

   ObservableCollection<foo> foos = new ObservableCollection<foo>();
    public MainWindow()
    {
        InitializeComponent();

        foos.Add(new foo("a","c1", new ObservableCollection<bar> { new bar("1"), new bar("2"), new bar("3"), new bar("4") }));
        foos.Add(new foo("b", "c2", new ObservableCollection<bar> { new bar("10"), new bar("20"), new bar("30"), new bar("40") }));
        foos.Add(new foo("c", "c3", new ObservableCollection<bar> { new bar("100"), new bar("200"), new bar("300"), new bar("400") }));
        foos.Add(new foo("d", "c4", new ObservableCollection<bar> { new bar("1000"), new bar("2000"), new bar("3000"), new bar("4000") }));

        tv.ItemsSource = foos; 
    }

models:

public class foo
{
    public foo(string sName,string cont, ObservableCollection<bar> oc)
    {
        Name = sName;
        OC = oc;
        Cont = cont;
    }
    public string Name { get; set; }
    public string Cont { get; set; }
    public ObservableCollection<bar> OC { get; set; }
}



public class bar
{
    //ObservableCollection<string> oc
    public bar(string sName)
    {
        Name = sName;
    }
    public string  Name { get; set; }
}

as u can see(after running it) the results(for t1,t2,t3) are, to say the least, strange.

I'll be very grateful if anyone could explain it, and come with a solution.

thanx in advance

g