私は、各列が可能な値のドロップダウンリストに関連するテーブルのコントロールテンプレートを作成しようとしており、各行には「選択された値」という固有のセットがあります。それは次のようになります。継承する各クラスタイプごとに異なる継承された静的メンバーですか?
<DataTemplate x:Key="ColourCellDataTemplate">
<local:ColourDictionary Key="{TemplateBinding Content}">
<local:ColourDictionary.ContentTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource EditableTextCell}" Text="{Binding ColourName}" />
</DataTemplate>
</local:ColourDictionary.ContentTemplate>
</local:ColourDictionary>
</DataTemplate>
私は約10これらのクラスのを持っている、との初期化それぞれ異なるデータテーブルとソートキーが、すべての基礎となる操作やイベントが同じです。
これらの各クラス、そのクラスのすべてのインスタンス間で共有され、独自の静的キャッシュされたDataViewメンバー、持っている:あなたが見ることができるように
public class ColourDictionary : DataTableDictionary
{
private static DataView cachedView;
public ColourDictionary(){ }
protected override DataView createView()
{
if (CachedView == null)
{
CachedView = base.buildView(table:((App)App.Current).ApplicationData.Colours,
keyField:"ColorCode");
}
return CachedView;
}
}
を - 基本クラスで使用するためのDataViewを作成するために行くとき継承しているさまざまなクラスがそれぞれのビューを渡すことを可能にする仮想メソッドを使用しますが、各クラスは独自の静的キャッシュを追跡する必要があります。
"CreateView()"は新しいDataViewを返すだけで済み、一度呼び出されると基本クラスがそのまま使用するように、このキャッシュが基本クラスに保持できるロジックであることを期待していました。各クラスの継承クラスのための独自のキャッシュです。
ソリューション
は、2つの非常に良い、非常に有効なアイデアをどうもありがとうございます。私はあなたに両方のupvotesを得ることを願っています。私は簡潔さのために吸盤であるので、私は後者の解決策に行きました。
public class CATCodeDictionary : DataTableDictionary<CATCodeDictionary>
{
protected override DataTable table { get { return ((App)App.Current).ApplicationData.CATCodeList; } }
protected override string indexKeyField { get { return "CatCode"; } }
public CATCodeDictionary() { }
}
public class CCYDictionary : DataTableDictionary<CCYDictionary>
{
protected override DataTable table { get { return ((App)App.Current).ApplicationData.CCYList; } }
protected override string indexKeyField { get { return "CCY"; } }
public CCYDictionary() { }
}
public class COBDictionary : DataTableDictionary<COBDictionary>
{
protected override DataTable table { get { return ((App)App.Current).ApplicationData.COBList; } }
protected override string indexKeyField { get { return "COB"; } }
public COBDictionary() { }
}
etc...
基本クラス
public abstract class DataTableDictionary<T> : where T : DataTableDictionary<T>
{
private static DataView _IndexedView = null;
protected abstract DataTable table { get; }
protected abstract string indexKeyField { get; }
public DataTableDictionary()
{
if(_IndexedView == null)
{
_IndexedView = CreateIndexedView(table.Copy(), indexKeyField);
}
}
private DataView CreateIndexedView(DataTable table, string indexKey)
{ // Create a data view sorted by ID (keyField) to quickly find a row.
DataView dataView = new DataView(table);
dataView.Sort = indexKey;
return dataView;
}
2つの素晴らしいソリューション。私はそれが簡潔であるためにこれが好きです。 – Alain
@Alain - 編集に感謝します。 – Henrik