2016-09-28 1 views
1

다른 DataTemplate을 내, 유형, 객체 속성을 템플릿하려는 :는 나는이처럼 데이터 그리드에 대한 뷰 모델이

public class Cell 
{ 
    public CellValue CellValue { get; } 
} 

그리고 CellValue 속성은 여러 종류의 수 있습니다 :

public class CellValue 
{ 
    public double Value { get; } 
} 

public class TwoValueCell : CellValue 
{ 
    public double Value2 { get; } 
} 

DataGrid는 ItemsSource를 내 행 및 셀 목록에 바인딩합니다.

DataGrid는 예상대로 데이터를 바인딩합니다. 각 DataGridCell에 셀을 가져옵니다. 그래서 나는 (지금은 지역의 자원)에이 같은 자원의 스타일을 가지고 : 내가보기 모델 유형에 바인딩 된 템플릿을 가지고 좋아

<DataGrid.Resources> 
    <Style TargetType="{x:Type DataGridCell}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type DataGridCell}"> 
        <ContentPresenter x:Name="ContentHost" 
          Margin="{TemplateBinding Padding}"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <DataTemplate DataType="{x:Type viewModel:Cell}"> 
     <Grid> 
      <TextBlock Text="{Binding CellValue}"/> 
     </Grid> 
    </DataTemplate> 
</DataGrid.Resources> 

... 질문은

: 대신 바인딩의 TextBlock에서 다른 DataTemplate을 팽창 시켜서 특정 CellValue 유형으로 다시 정의 할 수 있습니다. 그것은 다음과 같을 수 psudocode에 즉

:

<DataTemplate DataType="{x:Type viewModel:Cell}"> 
    <Grid> 
     <DataTemplate DataType="{x:Type viewModel:CellValue}"> 
      <TextBlock Text="{Binding Value}"/> 
     </DataTemplate> 
    </Grid> 
</DataTemplate> 

하지만 어딘가 특정 CellValue 데이터 템플릿을 정의 할 필요가 ...

결론은 하나 개의 유형을 가지고있는 DataGridCell --- 및 해당 유형에 대한 데이터 템플릿을 가지고 ---하지만 다음 셀의 속성에 여러 특정 유형을 가지고, 나는 속성의 실제 유형을 기반으로 사용자 정의 데이터 템플릿을 정의하고 싶습니다.

어떤 도움이 필요합니까?

답변

1

ContentControl을 사용할 수 있습니다.

<DataTemplate ...> 
    <Grid> 
     <ContentControl DataContext="{Binding SomeProperty}"> 
     <ContentControl.ContentTemplate> 
      <DataTemplate ...> ... </DataTemplate> 
     </ContentControl.ContentTemplate> 
     </ContentControl> 
    </Grid> 
</DataTemplate> 
+0

예! 방금 시도했는데 작동 중입니다! 중첩 된 데이터 템플릿에서도 사용자 정의 유형을 사용하고 있습니다 ... –

관련 문제