C# 如何获取数据模板';输入代码?

C# 如何获取数据模板';输入代码?,c#,.net,wpf,C#,.net,Wpf,我有一个DataTemplate,它使用x:Key来标识自己,如下所示: <DataTemplate x:Key="myTemplate > ... </DataTemplate> 您可以迭代所有资源并检查它们的密钥 public static string GetResourceInDictionary(ResourceDictionary dictionary, object item) { return (from Diction

我有一个DataTemplate,它使用x:Key来标识自己,如下所示:

<DataTemplate 
    x:Key="myTemplate
    >
    ...
</DataTemplate>

您可以迭代所有资源并检查它们的密钥

public static string GetResourceInDictionary(ResourceDictionary dictionary, object item)
{
    return (from DictionaryEntry key in dictionary
            where key.Value == item
            select key.Key.ToString()).FirstOrDefault();
}
然后将其用于:

string resourceKey = GetResourceInDictionary(this.Resources, myResource);

您可以迭代所有资源并检查它们的密钥

public static string GetResourceInDictionary(ResourceDictionary dictionary, object item)
{
    return (from DictionaryEntry key in dictionary
            where key.Value == item
            select key.Key.ToString()).FirstOrDefault();
}
然后将其用于:

string resourceKey = GetResourceInDictionary(this.Resources, myResource);
试试这个:

foreach (var key in this.Resources.Keys)
{
    Console.WriteLine(key);
}
试试这个:

foreach (var key in this.Resources.Keys)
{
    Console.WriteLine(key);
}