さまざまな条件に基づいてデータ テンプレートを設定する必要があるコントロールがあるため、DataTemplateSelector
割り当て先のコントロールのリソースからテンプレートを選択する を使用することにしました。
これは機能しますが、ここに問題があります。これらのリソースをファイルからリロードしており (ファイル システムの変更がある場合)、既にレンダリングされたコントロールを新しいテンプレートで更新する必要があります。DynamicResource
これは、セレクターの代わりに単に使用した場合に機能します。
セレクターは次のようになります。
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
//complex rules that select the template are here
//this unfortunately sets the template statically - if it changes, it won't get updated
return template;
}
したがって、リソースが変更された場合、セレクターは、使用した場合のように再評価されることはありませんDynamicResource
。
これを解決するアイデアがありました。ビューモデルでテンプレートを選択して、リソースが変更されたときにDataTemplate
プロパティを更新できるようにします。
私のViewModelの試み(単純化された例、INotifyPropertyChangeを適切に実装しています):
class MyViewModel {
public DataTemplate DataTemplate {get;set;}
public MyModel Model {
get {return _model;}
set {
if(_model != value) {
_model = value;
//Select new template here
//DUH: how do I access the resources as I would in DataTemplateSelector, when I don't have access to the container parameter?
}
}
}
}
私はこれを間違った方法で行っていると確信していますが、適切に行うにはどうすればよいですか? さまざまな理由から、ハードコードされた静的な場所からリソースにアクセスしたくありません。割り当てられているコンテナでそれらを見つける必要があります。
質問がわかりにくいので、お気軽に質問してください。明確にしようと思います。