Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Parameters are passed to SelectTemplate() of custom DataTemplateSelector. Why?

I'm using a CellTemplateSelector to conditionally display a checkmark in a DataGrid Column.

The method SelectTemplate(object item, DependencyObject container) of my DataTemplateSelector class is called but the parameter item is null instead of the expected DataRowView-object.

Here is the XAML-Code. Items in Property ItemsSource of DataGrid is a DataTable, which works fine for the other columns. (I work with Visual Studio Express 2010)

...
<Window.Resources>        
        <DataTemplate x:Key="CheckedTemplate">
            <Path Width="16" Height="16" Margin="6,0,0,0" 
            x:Name="CheckMark" SnapsToDevicePixels="False"
            Stroke="Green" Fill="Green" StrokeThickness="1"
            Data="M 12.4227,0.00012207C 12.4867,0.126587 12.5333,0.274536 
            12.6787,0.321411C 9.49199,3.24792 6.704,6.57336 
            4.69865,10.6827C 4.04399,11.08 3.47066,11.5573 2.83199,
            11.9706C 2.09467,10.2198 1.692,8.13196 3.8147e-006,
            7.33606C 0.500004,6.79871 1.31733,6.05994 1.93067,6.2428C 
            2.85999,6.51868 3.14,7.9054 3.60399,8.81604C 5.80133,
            5.5387 8.53734,2.19202 12.4227,0.00012207 Z " />
        </DataTemplate>
        <DataTemplate x:Key="UncheckedTemplate">
        </DataTemplate>

        <local:CheckmarkTemplateSelector x:Key="CheckmarkTemplateSelector"  CheckedTemplate="{StaticResource CheckedTemplate}" UncheckedTemplate="{StaticResource UncheckedTemplate}" />
    </Window.Resources>
...
<DataGrid ItemsSource="{Binding Items, Mode=OneWay}"  AutoGenerateColumns="False">            
    <DataGrid.Columns>
        <DataGridTextColumn Header="No" Binding="{Binding no}" IsReadOnly="True" />
        <DataGridTextColumn Header="Name" Binding="{Binding name}" IsReadOnly="True" />
        <DataGridTemplateColumn Header="Selected" CellTemplateSelector="{StaticResource CheckmarkTemplateSelector}" />
    </DataGrid.Columns>
</DataGrid>
...

Any help is appreciated. Thanks in advance.

like image 328
sannoble Avatar asked Nov 24 '10 15:11

sannoble


1 Answers

Are you sure it's always passed null, or just the first time? The CellTemplateSelector is called once with a null item when setting up the logical tree, then called once per data item with item passed the bound object. You may just be failing on the first null.

See also this question: Why is the SelectTemplate Method run 2 times in debug mode?

like image 193
Philip Rieck Avatar answered Nov 05 '22 20:11

Philip Rieck