0
Answer

Using HeaderDataTemplateSelector to show more then 1 column

Robert Thompson

Robert Thompson

14y
3k
1

I create a simple example using HeaderDataTemplateSelector & DataTemplateSelector.  The problem that I am having is that I can only get 1 Column Header to display.  I created both the Selector classes.  I would like to use different collections of data on the same ListView Grid.

The HeaderDataTemplateSelector is only firing 1 time.

<Window x:Class="ItemSelectorCell.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ItemSelectorCell"
    Title="Window1" Height="300" Width="300" DataContext="{Binding RelativeSource={RelativeSource self}}" >

    <Window.Resources>
            <!-- Display Header-->
            <DataTemplate x:Key="mHeaderTemplate1">
                   <TextBlock Text="Test 1"/>
            </DataTemplate>

            <DataTemplate x:Key="mHeaderTemplate2">
                <TextBlock Text="Test 2"/>
            </DataTemplate>

            <DataTemplate x:Key="mHeaderTemplate2">
                <TextBlock Text="Test 3"/>
            </DataTemplate>

            <DataTemplate x:Key="mHeaderTemplate4">
                <TextBlock Text="Test 4"/>
            </DataTemplate>
            <!-- HeaderTemplateSelector -->
            <local:HeaderTemplateSelector x:Key="mHeaderTemplateSelector"
                mHeaderTemplate1="{StaticResource mHeaderTemplate1}"
                mHeaderTemplate2="{StaticResource mHeaderTemplate2}"                                          
                mHeaderTemplate3="{StaticResource mHeaderTemplate3}"                                          
                mHeaderTemplate4="{StaticResource mHeaderTemplate4}"/>

            <!-- Display Data -->
            <DataTemplate x:Key="mDataTemplate1">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Background="Blue" Text="{Binding Value}"/>
                </StackPanel>
            </DataTemplate>

            <DataTemplate x:Key="mDataTemplate2">
                <TextBlock Background="Green" Text="{Binding Value}"/>
            </DataTemplate>

            <DataTemplate x:Key="mDataTemplate3">
                <TextBlock Background="Green" Text="{Binding Value}"/>
            </DataTemplate>

            <DataTemplate x:Key="mDataTemplate4">
                <TextBlock Background="Green" Text="{Binding Value}"/>
            </DataTemplate>

            <!-- ItemTemplateSelector -->
            <local:ItemTemplateSelector x:Key="mDataTemplateSelector"
                mDataTemplate1="{StaticResource mDataTemplate1}"                                       
                mDataTemplate2="{StaticResource mDataTemplate2}"                                       
                mDataTemplate3="{StaticResource mDataTemplate3}"                                                                               
                mDataTemplate4="{StaticResource mDataTemplate4}"/>

    </Window.Resources>

    <ListView x:Name="listView" ItemsSource="{Binding SourceCollection}">
        <ListView.View>
            <GridView>
                <GridViewColumn
                    HeaderTemplateSelector="{StaticResource mHeaderTemplateSelector}"
                    CellTemplateSelector="{StaticResource mDataTemplateSelector}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Window>