Click here to Skip to main content
15,893,663 members
Articles / Desktop Programming / WPF

TreeListView

Rate me:
Please Sign up or sign in to vote.
4.85/5 (23 votes)
6 Apr 2008CPOL1 min read 199K   9.8K   59  
TreeView with multiple columns
<Window x:Class="SampleApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SampleApplication"
    Title="Main Window" Height="300" Width="500">
    <local:TreeListView AllowsColumnReorder="True">
        <!--Create an item template to specify the ItemsSource-->
        <local:TreeListView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}" />
        </local:TreeListView.ItemTemplate>
        <local:TreeListView.Columns>
            <!--Create the first column containing the expand button and the type name.-->
            <GridViewColumn Header="Name" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--The Expander Button (can be used in any column (typically the first one))-->
                            <local:TreeListViewExpander/>
                            <!--Display the name of the DataElement-->
                            <TextBlock Text="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!--Create a second column containing the number of children.-->
            <GridViewColumn Header="Children" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>                        
                        <!--Display the size of the DataElement-->
                        <TextBlock Text="{Binding Children.Count}" HorizontalAlignment="Right"/>                        
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!--Create a third column containing the brush of the material.-->
            <GridViewColumn Header="Brush" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--Border showing the actual color-->
                            <Border Background="{Binding Brush}" CornerRadius="2"
                                    Width="16" Height="16"
                                    BorderThickness="1" BorderBrush="DarkGray"/>
                            <!--Display the brush-->
                            <TextBlock Text="{Binding Brush}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </local:TreeListView.Columns>
        <!--Create some sample data-->
        <MaterialGroup>
            <MaterialGroup>
                <DiffuseMaterial Brush="Blue"/>
                <DiffuseMaterial Brush="Red"/>
                <SpecularMaterial Brush="Orange"/>
            </MaterialGroup>
            <EmissiveMaterial Brush="AliceBlue"/>
        </MaterialGroup>
    </local:TreeListView>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions