DataTemplate キーで奇妙な動作が発生しました。DataType が x:Type で指定され、x:Key が x:Static 参照で指定されている場合、x:Key は無視されます。それを説明するためにサンプルアプリを書きました。
XAML リソース:
<DataTemplate DataType="{x:Type wpfApplication1:TestDto}" x:Key="{x:Static wpfApplication1:DataKeys.TestDtoKey}" />
<DataTemplate x:Key="{x:Static wpfApplication1:DataKeys.TestDtoKey2}" />
<DataTemplate DataType="{x:Type wpfApplication1:TestDto}" x:Key="TestKey3" />
<DataTemplate DataType="wpfApplication1:TestDto" x:Key="{x:Static wpfApplication1:DataKeys.TestDtoKey4}" />
C#:
public class TestDto {}
public static class DataKeys
{
public static string TestDtoKey = "TestKey";
public static string TestDtoKey2 = "TestKey2";
public static string TestDtoKey4 = "TestKey4";
}
アプリケーションを起動します。デバッガーで this.Resources.Keys を参照してください。
{DataTemplateKey(WpfApplication1.TestDto)} object {System.Windows.DataTemplateKey}
"TestKey2" object {string}
"TestKey3" object {string}
"TestKey4" object {string}
ご覧のとおり、最初のケースでは x:Key は無視されます!
誰かが何が起こっているのか説明できますか? ドキュメント ( http://msdn.microsoft.com/en-us/library/system.windows.datatemplate.datatype.aspx ) には、 x:Key を設定するとリソース キーが指定したものに設定されることが明確に示されています。