2016-12-14 40 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> 
相關問題