我已经为自定义类创建了一个DataTemplate
,该类应该用于显示文件类型的内容。
以下是放置在窗口资源字典中的示例:
<DataTemplate DataType="namespace:Handle">
<Border>
<TextBlock>This is a static data template</TextBlock>
</Border>
</DataTemplate>
DataGridTemplateColumn
CellTemplate
是否可以按类型而不是通过 x:Key
引用给定模板?
如果我使用类似的东西,它可以通过 Key 工作
<DataGridTemplateColumn Header="The file name" CellTemplate="{StaticResource myTemplate }" />
没问题,但是我可以进行类型绑定(bind)吗?
DataGrid 没有自动生成的列,并且并非每个列都应使用上面的模板进行模板化。
编辑:这是对@Anatoliy Nikolaev的回应:我认为你的答案并不是我所期望的解决方案,因为我只是使用了一把 key 。以下是概述该行为的示例:
<DataTemplate x:Key="{x:Type system:Object}" DataType="{x:Type dataTemplateOnType:Handle}">
<Border>
<TextBlock>This is a static data template</TextBlock>
</Border>
</DataTemplate>
我可以通过以下方式引用:
<DataGridTemplateColumn Header="The file name" CellTemplate="{StaticResource {x:Type system:Object}}" />
这仍然只是一个复杂的 key 类型。
最佳答案
不,这是不可能的。
您的 DataTemplate 必须是实例化对象才能将其分配给 CellTemplate 属性。这就是为什么您必须在 x:Key
的帮助下引用 DataTemplate 实例。
关于c# - DataTemplate 是否可以按类型应用于 DataGridTemplateColumn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513920/