5

私はC#コードで独自のDataTemplateを作成するスニペットを作成しました。そして、私はそれをdatagridカラムの編集テンプレートに追加します。 私がobject templateContent = tc.CellTemplate.LoadContent ();を呼び出すと、アプリケーションがクラッシュし、「FrameworkElementFactoryはこの操作のために密封されたテンプレートになければなりません」という例外がスローされます。 これは私のデータ・テンプレートを作成するコードです。FrameworkElementFactoryはこの操作のために密封されたテンプレート内にある必要があります

public override DataTemplate GenerateCellTemplate (string propertyName) 
    { 
     DataTemplate template = new DataTemplate (); 
     var textBlockName = string.Format ("{0}_TextBlock", propertyName); 
     FrameworkElementFactory textBoxElement = new FrameworkElementFactory (typeof (TextBlock), textBlockName); 
     textBoxElement.SetBinding (TextBlock.TextProperty, new Binding (propertyName)); 
     template.VisualTree = textBoxElement; 
     Trigger trigger = new Trigger (); 
     return template; 
    } 

答えて

13

リフレクタにフレームワークテンプレートコードが反映されています。そして、私はtc.CellTemplate.LoadContent()がFrameworkTemplateクラスの "_sealed"という名前のプライベートフィールドに関係していることを発見しました。

私はこのフィールドを設定値に設定しました。このメソッドを呼び出すと、問題は解決されます。あなたは岩

public override DataTemplate GenerateCellTemplate (string propertyName) 
{ 
    DataTemplate template = new DataTemplate (); 
    var textBlockName = string.Format ("{0}_TextBlock", propertyName); 
    FrameworkElementFactory textBoxElement = new FrameworkElementFactory (typeof (TextBlock), textBlockName); 
    textBoxElement.SetBinding (TextBlock.TextProperty, new Binding (propertyName)); 
    template.VisualTree = textBoxElement; 
    Trigger trigger = new Trigger (); 

    // This solves it! 
    template.Seal(); 

    return template; 
} 
+2

:ここ

はソリューションです!ありがとう! – Marc

+2

私はTelerik GridViewを使用して動的にDataTemplatesを作成しています。それを動作させるにはSeal()を呼び出す必要がありました。なぜなのかご存知ですか?なぜこれを使うべきなのかの例は見つけられませんでしたか? –

関連する問題