【问题标题】:DataGrid- How to access Row.IsSelected from within a Column (in xaml)DataGrid-如何从列中访问 Row.IsSelected(在 xaml 中)
【发布时间】:2016-12-14 09:00:42
【问题描述】:
【问题讨论】:
标签:
wpf
xaml
data-binding
wpfdatagrid
【解决方案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>