I'm trying to ScrollIntoView of a DataGrid and highlight the specific row and column in another color. The ScrollIntoView works in jumping to the right spot. The highlight does not. Here is what I use to jump to the position:
public void ShowSelection(int row, int column)
{
dtGridReads.SelectedItem = dtGridReads.Items[row];
dtGridReads.SelectedItem = dtGridReads.Columns[column];
dtGridReads.UpdateLayout();
dtGridReads.ScrollIntoView(dtGridReads.Items[row], dtGridReads.Columns[column]);
}
Here is my WPF 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"
CanUserAddRows="False" CanUserDeleteRows="False" FrozenColumnCount="1"
GridLinesVisibility="None" Style="{StaticResource DataGridStyle_Blue}" ScrollViewer.ScrollChanged="dtGridReads_ScrollChanged">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger >
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
Thanks.
What does your ItemsSource look like for this?
Your trigger is trying to bind to the IsSelected property of the underlying object for each DataGridRow, so if that object doesn't have that property then you won't get any results. Setting the SelectedItem on the DataGrid won't affect your trigger as it is written above.
EDIT: I put together a quick sample to test. As expected, you're trying to bind to the wrong thing (it also looks like you have the wrong target as well if the goal is to highlight a specific cell in red).
If you replace the entire DataGrid.RowStyle block with this, it will work as expected:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>