c# - DataTemplate 是否可以按类型应用于 DataGridTemplateColumn?

标签 c# wpf xaml

我已经为自定义类创建了一个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 类型。

最佳答案

不,这是不可能的。

参见http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn.celltemplate(v=vs.110).aspx供引用。

您的 DataTemplate 必须是实例化对象才能将其分配给 CellTemplate 属性。这就是为什么您必须在 x:Key 的帮助下引用 DataTemplate 实例。

关于c# - DataTemplate 是否可以按类型应用于 DataGridTemplateColumn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513920/

相关文章:

wpf - 如何预渲染 View ?

c# - 通过 SignalR Core 访问数据库上下文

c# - 如何在执行某些方法时在 aspx 页面(WebForms)中显示等待过程

c# - 在C#winforms中通过动态代码执行不同namspace的方法

wpf - 如何在增加窗口高度的同时增加 WPF 中控件的高度?

c# - WPF MVVM 数据模型

.net - 有没有办法在不命名目标控件的情况下使用 Wpf 标签的目标?

c# - .Net 3.5 VS 2008 无效 token 'out' 错误

c# - 如何将常规文本附加到已经具有绑定(bind)到其文本属性的 TextBlock?

c# - 您可以设置 WPF TextBlock 的各个部分的样式吗?如果可以,如何设置?