2016-09-28 4 views
1

別のDataTemplate内、タイプによって、オブジェクトのプロパティをテンプレートにしたい:は、私はこのようなものですDataGridのビューモデルを持っている

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にCellを取得します。だから私は(今のところローカルリソース)は、このような資源でスタイルを持っている:私は、ビューモデルタイプにバインドされたテンプレートを持つよう

<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データテンプレートを定義する必要があります...

一番下の行は、1種類を持っていますDataGridCell ---とそのタイプのデータテンプレートを持っている - しかし、セルのプロパティにいくつかの特定の型を持っている、私はカスタムデータテンプレートをPROPERTYの実際のタイプに基づいて定義したい。

助けが必要ですか?

答えて

1

ContentControlを使用できます。

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

はい!私はちょうどそれを試み、それは働いている!ネストされたデータテンプレートのカスタムタイプも使用しています... –

関連する問題