0

リソース ディクショナリのジェネリック クラスのさまざまなサブクラスに同じデータ テンプレートを指定したいと考えています。これを達成する簡単な方法は何ですか?

class Base<T> {}

class DerivedInt : Base<int> {}

class DerivedDouble : Base<double> {}

<ResourceDictionary>
    <DataTemplate DataType="{x:Type local:DerivedInt}">
        <!--content goes here-->
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:DerivedDouble}">
        <!--same content as above goes here-->
    </DataTemplate>
</ResourceDictionary>

だから私は実際にデータテンプレートの同じ内容を再度入力せずに指定したい

4

1 に答える 1

0

すべての型に同じテンプレートを使用する場合は、共通の型に変換されたプロパティ値を文字列またはテンプレートのオブジェクトとして使用することになると思います。

テンプレートにバインドするプロパティを持つ派生オブジェクトのラッパー クラスを作成します。これらのラッパー クラスには 1 つのデータ テンプレートが必要です

public class DerivedWrapper
{
   //Properties you want to bind

   public DerivedWrapper(DerivedInt dInt)
   {
      //Set the properties 
   }

   public DerivedWrapper(DerivedDouble dDouble)
   {
      //Set the properties
   }
}

その後

<ResourceDictionary>
    <DataTemplate DataType="{x:Type local:DerivedWrapper}">
        <!--content goes here-->
    </DataTemplate>
</ResourceDictionary>
于 2013-09-10T08:16:53.483 に答える