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"라는 private 필드와 관련이 있다는 것을 알았습니다.

그런 다음 필드 값을 설정해야하며이 메서드를 호출하면 문제가 해결됩니다. 당신은 바위

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()을 호출하여 작동하도록했습니다. 왜 그런지 알아? 이것이 왜 사용되어야하는지에 대한 예를 찾을 수 없었습니다. –

관련 문제