12

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 を設定するとリソース キーが指定したものに設定されることが明確に示されています。

4

2 に答える 2

1

それが役立つかどうかはわかりませんが、クラスの各プロパティに {get;set;} を追加してみてください。WPF の一部のスタッフ (バインディングなど) は、それらがないと機能しません。ここも同じ話かも…

public static class DataKeys
{
  public static string TestDtoKey { get { return "TestKey"; } set; }
  public static string TestDtoKey2 { get { return "TestKey2"; } set; }
  public static string TestDtoKey4 { get { return "TestKey4"; } set; }
}
于 2013-10-18T15:46:54.153 に答える