DataTemplate 可以按类型应用于 DataGridTemplateColumn 吗? [英] Can a DataTemplate be applied by type to a DataGridTemplateColumn?

查看:20
本文介绍了DataTemplate 可以按类型应用于 DataGridTemplateColumn 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为自定义类创建了一个 DataTemplate,该类应该用于显示文件类型的内容.

I've created a DataTemplate for a custom class which should be used to display the contents of the file type.

这是一个放置在窗口资源字典中的示例:

Here is a sample one placed in the window's resource dictionary:

<DataTemplate DataType="namespace:Handle">
    <Border>
        <TextBlock>This is a static data template</TextBlock>
    </Border>
</DataTemplate>

DataGridTemplateColumn CellTemplate 是否可以通过类型而不是 x:Key 来引用给定的模板?

Is it possible for a DataGridTemplateColumn CellTemplate to reference the given template by type and not by an x:Key?

如果我使用类似的东西,它可以通过 Key 工作

It works by Key if I use something like

<DataGridTemplateColumn Header="The file name" CellTemplate="{StaticResource myTemplate }" />

没关系,但我可以做类型绑定吗?

that's fine, but can I do a type binding?

DataGrid 没有自动生成的列,并且不是每个列都应该由上面的模板模板化.

The DataGrid has no auto generated columns and not every column should be templated by the template above.

这是对@Anatoliy Nikolaev 的回应:我认为您的回答完全不是我预期的解决方案,因为我只是在使用密钥.下面是一个概述行为的示例:

This is a response to @Anatoliy Nikolaev : I think your answer is quite not the solution I was expected as I am just using a key. Here is a sample that outlines the behaviour:

<DataTemplate x:Key="{x:Type system:Object}" DataType="{x:Type dataTemplateOnType:Handle}">
    <Border>
        <TextBlock>This is a static data template</TextBlock>
    </Border>
</DataTemplate>

我可以通过以下方式引用:

I can reference this by:

<DataGridTemplateColumn Header="The file name" CellTemplate="{StaticResource {x:Type system:Object}}" />

这仍然只是一个复杂的密钥类型.

This remains just a complex key type.

推荐答案

这是一个成功的例子:

<Window.Resources>
    <DataTemplate x:Key="{x:Type this:TestData}"
                  DataType="{x:Type this:TestData}">

        <TextBox Text="{Binding Path=SomeProperty}" 
                 Background="Aquamarine" />
    </DataTemplate>
</Window.Resources>

...

<DataGridTemplateColumn Header="Test" 
                        Width="*"                                        
                        CellTemplate="{StaticResource {x:Type this:TestData}}" />

如果在 CellTemplate 中写入 DynamicResource 它将不起作用(正如我最初尝试的那样).

If in CellTemplate write DynamicResource it's will not work (as I tried at first).

在这种情况下,这一行:

In this case this line:

x:Key="{x:Type this:TestData}"

当然,没有她的例子是行不通的.虽然,当您缺少 StyleDataTemplate 的键时,此构造:

necessarily, without her example will not work. Although, when you're missing a key for Style or DataTemplate, this construction:

<DataTemplate TargetType="{x:Type local:MyType}">

自动转换成这样:

<DataTemplate x:Key="{x:Type local:MyType}" TargetType="{x:Type local:MyType}">

这意味着 Style/DataTemplate 将显式用于此类型的所有控件,因为在 ResourceDictionaries 中不能是没有键的元素,而是这样做是为了简化结构 MSDN.

This means that the Style/DataTemplate will be used explicitly for all controls of this type because in ResourceDictionaries can not be elements without keys, rather this is done to simplify the structure MSDN.

为什么在这种情况下没有钥匙就不能工作?我认为这是一个错误,或者有其他限制阻止他正常工作,但它应该可以在没有密钥的情况下工作.

Why in this situation does not work without a key? I think it is a bug, or have other restrictions that prevent him from working as it should, but it should work without a key.

这篇关于DataTemplate 可以按类型应用于 DataGridTemplateColumn 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆