我想出了如何突出感兴趣的行。我可以获取我感兴趣的列的索引并使用ScrollIntoView跳转到它。但是,它不会立即弹出给用户。我想突出显示该列,或更改ColumnHeaderStyle。我似乎无法弄清楚如何在xaml或后面的代码中执行此操作。
另一种选择是不突出显示整行和列标题,而只突出显示感兴趣的单元格。我宁愿这样做,但也无法理解。
我当前的DataGrid如下所示:
<DataGrid x:Name="dtGridReads" AutoGenerateColumns="False"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode ="Standard"
EnableColumnVirtualization="True"
EnableRowVirtualization="True"
ScrollViewer.IsDeferredScrollingEnabled="True"
CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="True"
ItemsSource ="{Binding}" Block.TextAlignment="Center"
AlternatingRowBackground="#F1F1F1" RowBackground="White"
CanUserAddRows="False" CanUserDeleteRows="False" FrozenColumnCount="1"
GridLinesVisibility="None" ScrollViewer.ScrollChanged="dtGridReads_ScrollChanged">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="red" />
<Setter Property="BorderThickness" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
我的代码背后:
public void ShowSelectedCell(int row, int column)
{
//dtGridReads.SelectedItem = dtGridReads.Items[row];
//dtGridReads.SelectedItem = dtGridReads.Columns[column];
//dtGridReads.CurrentColumn = dtGridReads.Columns[column];
dtGridReads.ScrollIntoView(dtGridReads.Items[row], dtGridReads.Columns[column]);
}
感谢。
答案 0 :(得分:0)
假设您需要根据所选的\突出显示的单元格高亮显示数据gid列标题....
为DataGrid
创建DataGridColumnHeader
范围样式。在样式中包含一个数据触发器,用于检查列标题的内容和当前单元格列标题的内容是否相等...这意味着列标题属于当前单元格所属的列。如果是这样,我们改变标题的背景颜色。
<toolkit:DataGrid x:Name="MyDataGrid"
IsReadOnly="True"
AutoGenerateColumns="False">
<toolkit:DataGrid.Resources>
<local:EqualityConverter x:Key="EqualityConverter"/>
<Style TargetType="{x:Type toolkit:DataGridColumnHeader}">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualityConverter}">
<Binding Path="CurrentCell.Column.Header"
RelativeSource="{RelativeSource
AncestorType={x:Type toolkit:DataGrid}}"/>
<Binding Path="Content"
RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</toolkit:DataGrid.Resources>
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Key"
Binding="{Binding Key, Mode=OneWay}"></toolkit:DataGridTextColumn>
<toolkit:DataGridTextColumn Header="Value"
Binding="{Binding Value, Mode=OneWay}"></toolkit:DataGridTextColumn>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
EqualityConverter
只是一个多绑定转换器,用于检查绑定值是否相等...
public class EqualityConverter : IMultiValueConverter
{
public object Convert(
object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values != null)
{
for (var i = 1; i < values.Count(); i++)
{
if (values[i] == null || !values[i].Equals(values[i-1]))
{
return false;
}
}
return true;
}
return false;
}
public object[] ConvertBack(
object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
如果有帮助,请告诉我。