1

すぐに飛び込んで、コードを説明しましょう。

            FrameworkElement par = list;
        while((par = par.Parent as FrameworkElement) != null) {
            grid.Resources.MergedDictionaries.Add(par.Resources);
        }
        grid.DataContext = list.DataContext;
        if(rootparent is ContentControl) {
            (rootparent as ContentControl).Content = null;
        } else if(rootparent is Decorator) {
            (rootparent as Decorator).Child = null;
        } else if(rootparent is Panel) {
            rootindex = (rootparent as Panel).Children.IndexOf(list);
            (rootparent as Panel).Children.RemoveAt(rootindex);
        }
        grid.Children.Add(list);

したがって、基本的に、テンプレート化されたコントロールは元のウィンドウから移動し、バックグラウンドでインスタンス化されたグリッドに移動します。そのデータコンテキストは正常に転送されます (切断されたときに null になり、グリッドに参加したときに元のオブジェクトに戻るのを見ました) が、テンプレートは転送されません。理由はわかりません。上部で、すべてのリソース ディクショナリを最上位の親にコピーし、それらを新しいグリッドにマージしているためです。

そのため、テンプレートを再適用する際に何かが欠けています。

4

1 に答える 1

0

リソースは、単に参照するだけでなく、新しいコンテナーに複製する必要がありました。

FrameworkElement par = list;
while((par = par.Parent as FrameworkElement) != null) {
    DictionaryEntry[] resources = new DictionaryEntry[par.Resources.Count];
    par.Resources.CopyTo(resources, 0);
    var res = new ResourceDictionary();
    foreach(DictionaryEntry ent in resources)
        res.Add(ent.Key, ent.Value);
    grid.Resources.MergedDictionaries.Add(res);
}
于 2013-02-06T14:20:16.690 に答える