0

I'm trying to bind to properties on my GridView's DataContext and use a DataTemplateSelector to assign the correct template to the cell, but I can't seem to find the correct way to do this. Binding to DisplayMemberBinding overrides the template selector, but setting the CellTemplateSelector property binds to the DataContext rather than the properties I want to select templates for.

This answer seems to describe exactly what I'm looking for, but I'm having trouble finding information on how to implement what it describes: https://stackoverflow.com/a/12519433/1756960 .

This is what I tried using that isn't working (simplified for posting):

    <ListView ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name"
                                DisplayMemberBinding="{Binding Name}"
                                CellTemplateSelector="{StaticResource ContentTypeTemplateSelector}" />
                <GridViewColumn Header="Data"
                                DisplayMemberBinding="{Binding}"
                                CellTemplateSelector="{StaticResource ContentTypeTemplateSelector}" />
            </GridView>
        </ListView.View>
    </ListView>
Community
  • 1
  • 1
Josh
  • 632
  • 7
  • 13

1 Answers1

0

The first thing I would recommend doing is to differentiate your Content Template selectors in one of two ways. The First is to simply have more than one template selector class. the second is to have two instances, whose templates are assign different bindings.

<Resources>
    <ns:TemplateSelector x:Key="NameTemplateSelector">
        <ns:TemplateSelector.Template1>
            <DataTemplate>
                <!-- Something bound to Name -->
            </DataTemplate>
        </ns:TemplateSelector.Template1>
    </ns:TemplateSelector>

    <ns:TemplateSelector x:Key="DataTemplateSelector">
        <ns:TemplateSelector.Template1>
            <DataTemplate>
                <!-- Something bound to Data -->
            </DataTemplate>
        </ns:TemplateSelector.Template1>
    </ns:TemplateSelector>

The reference to Attached Properties (see MSDN) would have you make a property, attach it to the template selector, and then access that data from the TemplateSelector's code.

David
  • 10,458
  • 1
  • 28
  • 40