2016-12-14 54 views
0

我有一个DataGrid,有几列和几行。DataGrid - 如何从列中访问Row.IsSelected(in xaml)

对于选定的行,我想为每列显示组合框(绑定到字符串列表)。

对于没有选中的行,我想显示一个带有选定字符串的TextBlock。

我打算使用DataGridColumnTemplate内的绑定(也许像这里的样式How to display combo box as textbox in WPF via a style template trigger?)。我如何去从列的CellTemplate中去“Row.IsSelected”?我想我需要去视觉树上的行?

回答

1

我想我需要做的视觉树行?

是的,你可以使用的RelativeSource绑定到你的CellTemplate父DataGridRow的任何属性:

<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>