How to achive a Dynamic-Static-Dynamic structure in a TreeVi

Mar 12 2013 5:51 AM

I'm working on a software to display some Database structure. I'm using SMO to retrieve Information about tables, Vies an Procedures. The Object Structure is as follows:

- Database
    - Tables
    - Views
    - Procedures

I retrieve some database objects, which contain Collections of Views, Tables and Procedures. 

What I want to do know is populate a TreeView with the different Collections. The first Level of TreviewItems would be dynamic and display the Databases. In the second level (under the databases) would be three static Items. These would be named "Tables", "Views" and "Procedures". Each of these subitems should be populated by one of the Collections in the database object. I've tried the following code:

                <TreeView Grid.Row="1" Margin="20,0,10,20" x:Name="lstTable" ItemsSource="{Binding Databases}">
                    <TreeView.ItemTemplate>
                        <DataTemplate>
                            <TreeViewItem Header="{Binding Name}">
                                <TreeViewItem.Items>
                                    <TreeViewItem Header="Tables" ItemsSource="{Binding Tables}">
                                        <TreeViewItem.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Name}" />
                                            </DataTemplate>
                                        </TreeViewItem.ItemTemplate>
                                    </TreeViewItem>
                                    <TreeViewItem Header="Views" ItemsSource="{Binding Views}">
                                        <TreeViewItem.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Name}" />
                                            </DataTemplate>
                                        </TreeViewItem.ItemTemplate>
                                    </TreeViewItem>
                                    <TreeViewItem Header="Procedures" ItemsSource="{Binding Procedures}">
                                        <TreeViewItem.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Name}" />
                                            </DataTemplate>
                                        </TreeViewItem.ItemTemplate>
                                    </TreeViewItem>
                                </TreeViewItem.Items>
                            </TreeViewItem>
                        </DataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>

This code works nearly as expected but has the problem that you can't select any items. It seems like the problem occurs due to the TreeViewItems that I'm adding to the TreeView. In another thread I've read, that you should just add TextBoxes (or other controls) since the TreeView automatically surrounds them with a TreeViewItem. But How can I add Subitems to these TextBox-"Items"?

Thanks in advance :-)


Answers (2)