没有参数​​传递给定制DataTemplateSelector的SelectTemplate()。为什么?参数、DataTemplateSelector、SelectTemplate

2023-09-05 00:26:43 作者:流光若逝情若殇

我使用了一个 CellTemplateSelector 来有条件地显示在DataGrid列中的选中标记。

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

方法 SelectTemplate(对象项目,DependencyObject的容器)我的 DataTemplateSelector 类被称为但是参数产品空而不是预期的 DataRowView的 -object。

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.

下面是XAML- code。在地产的ItemsSource DataGrid中的项目是数据表,这对于其他列正常工作。 (我与Visual Studio防爆preSS工作2010 的)

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>
...

任何帮助是AP preciated。先谢谢了。

Any help is appreciated. Thanks in advance.

推荐答案

你确定它一直通过为空,或者仅仅是第一次?该 CellTemplateSelector 与空项目设置逻辑树时,则调用一次每个数据项与项目通过绑定的对象。你可能只是没有在第一个空。

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.

也看到这一问题:Why是SelectTemplate方法运行2次在调试模式?

 
精彩推荐
图片推荐