2012-06-23 2 views
1

DataGrid 항목 (행 및 셀)에 대한 VM 클래스를 만들었습니다.WPF DataGrid, 템플릿 열 및 가상화

public class ListGridCell : INotifyPropertyChanged 
{ 
    public ListGridCell(string Name) 
    { 
     // Init properties 
     this.Name = Name; 
     this.DataValue = null; 
     this.DataEditor = null; 
    } 

    public string Name { get; private set; } 

    private object _DataValue; 
    public object DataValue 
    { 
     get { return _DataValue; } 
     set { _DataValue = value; NotifyPropertyChanged("DataValue"); } 
    } 

    private FrameworkElement _DataEditor; 
    public FrameworkElement DataEditor 
    { 
     get { return _DataEditor; } 
     set { _DataEditor = value; NotifyPropertyChanged("DataEditor"); } 
    } 
    ... 
} 

DataGrid 열 및 VM은 코드에서 완전히 동적으로 내장되어 있습니다 : 셀의 VM 클래스는 다음과 같습니다. 는 I 템플릿 컬럼 (DataGridTemplateColumn)를 생성하고 (XAML 통해 도시)를 followind 템플릿에 CellTemplate을 설정

<StackPanel> 
    <TextBlock Text="{Binding Path=DataValue}" /> 
    <ContentControl Content="{Binding Path=DataEditor}" /> 
</StackPanel> 

모두 잘 작동 예상대로 DataGrid 처음에는 가득 표시 될 때. 자, 그리드를 스크롤하려고하면 다음 예외가 발생합니다.

지정된 요소는 이미 다른 요소의 논리적 하위입니다. 먼저 연결을 끊습니다.

이 예외는 그리드의 행의 가상화 및 세포 템플릿 내부 DataEditorContent 바인딩 함께 할 수있는 뭔가가. 행 가상화를 끄면 모든 것이 잘 작동하지만 그리드 성능은 매우 나빠져 옵션이 아닙니다.

DataGrid 가상화가 백그라운드에서 작동하는지, 행을로드/언로드 할 때 어떤 현상이 발생하며이 오류의 원인은 무엇인지 알고 계십니까? 해결 방법이 있습니까?

참고 :이 셀 편집기를 수동으로 만들고 초기화해야하기 때문에 셀 서식 파일의 셀 데이터 편집기에 대한 바인딩 (해결 방법은 여러 곳에서 제안 됨)을 사용할 수 없습니다.

답변

0

해결되었습니다.

DataGrid 가상화와 조합하여 Content 바인딩을 사용하면 분명히 어딘가의 ContentControl에있는 버그입니다.

ContentPresenter으로 전환하면 모든 것이 완벽하게 작동합니다.

근무 세포 템플릿은 다음과 같습니다

<StackPanel> 
    <TextBlock Text="{Binding Path=DataValue}" /> 
    <ContentPresenter Content="{Binding Path=DataEditor}" /> 
</StackPanel> 
+0

가능성이 버그는 여전히 윈도우 스토어 XAML에 존재하지만, 오류없이, https://connect.microsoft.com/VisualStudio/feedback/details/797231/content- 참조 lost-contentcontrol-bound-to-usercontrol-inside-virtualizing-stackpanel # tabs –