在 DataGrid 中突出显示 SelectedItem 的行
我正在尝试 ScrollIntoView 的 DataGrid 并以另一种颜色突出显示特定的行和列。 ScrollIntoView 的作用是跳转到正确的位置。亮点没有。这是我用来跳转到该位置的内容:
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]);
}
这是我的 WPF 数据网格:
<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>
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 ItemsSource 是什么样的?
您的触发器尝试绑定到每个 DataGridRow 的基础对象的 IsSelected 属性,因此如果该对象没有该属性,那么您将不会得到任何结果。在 DataGrid 上设置 SelectedItem 不会影响您的触发器,正如上面所写的。
编辑:
我整理了一个快速样本进行测试。正如预期的那样,您试图绑定到错误的东西(如果目标是以红色突出显示特定单元格,那么您看起来也有错误的目标)。
如果用此替换整个 DataGrid.RowStyle 块,它将按预期工作:
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 拥有的属性来完成,例如
DataGridRow.IsSelected
或DataGridCell.IsSelected
Row
Column
现在,您必须告诉 datagrid 有关样式和所选项目的信息
现在您您的 ModelView 中应该有一个名为
SelectedItem
的属性,其中包含数据网格中您想要选择的对象。This can also be done using DataGrid owns properties like
DataGridRow.IsSelected
orDataGridCell.IsSelected
Row
Column
Now you have to tell your datagrid about the style and about the selected item
Now you should have a property in your ModelView with the name of
SelectedItem
that contains the object in datagrid which you want to be selected.