2016-12-14 10 views
0

私はいくつかの列と行を持つDataGridを持っています。DataGrid - 列内からRow.IsSelectedにアクセスする方法(xaml内)

選択した行については、各列にコンボボックス(文字列のリストにバインドされている)を表示したいとします。

選択されていない行については、選択した文字列でTextBlockを表示したいとします。

私は、DataGridColumnTemplate内のバインディング(およびおそらくここのようなスタイルHow to display combo box as textbox in WPF via a style template trigger?)を使用してそれを行うことを目指しています。列のCellTemplate内から "Row.IsSelected"に行く方法は?行にビジュアルツリーを表示する必要があると思いますか?

答えて

1

私は行に視覚的なツリーを上げる必要があると思いますか?

<TextBlock Text="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> 

だから、このようなものが動作するはずです::

<DataGridTemplateColumn> 
<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <Grid> 
      <ComboBox x:Name="cmb"> 
       <ComboBoxItem>1</ComboBoxItem> 
       <ComboBoxItem>2</ComboBoxItem> 
       <ComboBoxItem>3</ComboBoxItem> 
      </ComboBox> 
      <TextBlock x:Name="txt" Text="..." Visibility="Collapsed" /> 
     </Grid> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True"> 
       <Setter TargetName="cmb" Property="Visibility" Value="Collapsed" /> 
       <Setter TargetName="txt" Property="Visibility" Value="Visible" /> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

はい、あなたはあなたのCellTemplateで親DataGridRowの任意のプロパティにバインドするRelativeSourceを使用することができます

関連する問題