没有参数传递给自定义 DataTemplateSelector 的 SelectTemplate()。为什么?
我使用 CellTemplateSelector
有条件地在 DataGrid 列中显示复选标记。
我的 DataTemplateSelector
类的方法 SelectTemplate(object item, DependencyObject container)
被调用,但参数 item 为 null,而不是预期的 DataRowView
-object 。
这是 XAML 代码。 DataGrid 的属性 ItemsSource
中的项目是一个 DataTable
,它适用于其他列。 (我使用 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>
...
任何帮助都会受到赞赏。提前致谢。
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.
你确定它总是传递 null,还是只是第一次?在设置逻辑树时,使用 null
item
调用CellTemplateSelector
一次,然后使用传递绑定对象的item
每个数据项调用一次。您可能只是在第一个空值上失败了。另请参阅这个问题: 为什么是 SelectTemplate 方法在调试模式下运行 2 次?
Are you sure it's always passed null, or just the first time? The
CellTemplateSelector
is called once with a nullitem
when setting up the logical tree, then called once per data item withitem
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?