WPF DataGrid 突出显示整行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36554806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid highlight entire row
提问by KSF
I'm using a WPF datagrid to display data. I'm trying to figure out how to highlight the entire row when an item is clicked, including the empty columns/space on the end if there is empty space on the end (I'm using dynamically generated columns). Note, I'm NOT trying to highlight just a particular cell. This http://imgur.com/7Xjoj2His what I'm seeing. The unused space at the end of the row is space where there is not currently a column, and is not highlighted. I would like for that space to be highlighted as well when the row is clicked. I've tried:
我正在使用 WPF 数据网格来显示数据。我试图弄清楚如何在单击项目时突出显示整行,如果末尾有空白,则包括末尾的空列/空格(我使用的是动态生成的列)。请注意,我不是要突出显示特定的单元格。这个http://imgur.com/7Xjoj2H就是我所看到的。行尾未使用的空间是当前没有列的空间,不会高亮显示。我希望在单击该行时也突出显示该空间。我试过了:
Setting the width to the last column of auto to fill the remaining space. This doesn't work for me because doing this disables the horizontal scroll bar, which I need since I'm using dynamically created columns.
将宽度设置为 auto 的最后一列以填充剩余空间。这对我不起作用,因为这样做会禁用水平滚动条,这是我需要的,因为我使用的是动态创建的列。
I've also tried adding a style for DataGridRows:
我还尝试为 DataGridRows 添加样式:
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Background"
Value="{StaticResource SelectedBrush}" />
</Trigger>
This kind of works, although, when the datagrid loads, or when my program is not in focus, it looks like this: http://imgur.com/EtTmBbHwhere only the last area of unused space is highlighted.
这种工作,虽然,当数据网格加载时,或者当我的程序没有聚焦时,它看起来像这样:http: //imgur.com/EtTmBbH,其中只有未使用空间的最后一个区域被突出显示。
If possible, I'd prefer not to workaround this by just adding a blank dummy column on the end because this leads to the same problems as just setting the last column width to auto above.
如果可能的话,我不想通过在最后添加一个空白的虚拟列来解决这个问题,因为这会导致与将最后一列的宽度设置为 auto 相同的问题。
Any suggestions?
有什么建议?
Edit:
编辑:
Here is the code for the DataGridRow style:
这是 DataGridRow 样式的代码:
<Style TargetType="{x:Type DataGridRow}"
x:Key="DataGridRowStyle">
<Setter Property="Background"
Value="White" />
<Style.Triggers>
<Trigger Property="AlternationIndex"
Value="1">
<Setter Property="Background"
Value="#efefef" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Background"
Value="{StaticResource RolloverBrush}" />
</Trigger>
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Background"
Value="{StaticResource SelectedBrush}" />
</Trigger>
</Style.Triggers>
</Style>
And the selected brush:
和选定的画笔:
<LinearGradientBrush x:Key="SelectedBrush"
StartPoint="0,0"
EndPoint="0,1">
<GradientStop Offset="0"
Color="#3399FF" />
</LinearGradientBrush>
The code for the datagrid:
数据网格的代码:
<DataGrid x:Name="JobListView"
AutoGenerateColumns="False"
ItemsSource="{Binding UnitStatusCollection, Mode=TwoWay}"
CanUserDeleteRows="False"
Style="{StaticResource JobGridViewStyle}"
SelectedIndex="{Binding JobsListViewSelectedIndex, Mode=TwoWay}"
SelectedItem="{Binding JobsListViewSelectedUnitStatusPair}"
Utility:DataGridColumnsBehavior.BindableColumns="{Binding DataGridColumns}"
ContextMenu="{StaticResource ListViewContextMenu}"
Margin="10,0"
Grid.Row="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
RowStyle="{StaticResource DataGridRowStyle}"
AlternationCount="2"
HorizontalGridLinesBrush="#5A5A5F"
VerticalGridLinesBrush="#5A5A5F">
回答by Anton Danylov
You may clear default highlight color, so that your SelectedBrush would be used instead:
您可以清除默认的高亮颜色,以便使用您的 SelectedBrush:
<DataGrid ...>
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#00000000"/>
</DataGrid.Resources>
</DataGrid>
回答by KSF
In order to make this work, I used Anton's suggestion:
为了完成这项工作,我使用了 Anton 的建议:
Also, a row in DataGrid consists of 2 parts, one of them is occupied by cells and the second one is not. You can set CellStyle also with the same SelectedBrush. In that case cells will be highlighted in the right color.
此外,DataGrid 中的一行由两部分组成,其中一个被单元格占据,第二个没有。您也可以使用相同的 SelectedBrush 设置 CellStyle。在这种情况下,单元格将以正确的颜色突出显示。
What I did was make a style for DataGridCellStyle and made a trigger for IsSelected:
我所做的是为 DataGridCellStyle 创建一个样式并为 IsSelected 创建一个触发器:
<Style x:Key="DataGridCellStyle"
TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="{StaticResource SelectedBrush}" />
</Trigger>
</Style.Triggers>
</Style>
Which paints the background of the cells the same color as the background of the selected row. This is in addition to having:
它将单元格的背景绘制为与所选行的背景相同的颜色。除此之外,还有:
<Style TargetType="{x:Type DataGridRow}"
x:Key="DataGridRowStyle">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Background"
Value="{StaticResource SelectedBrush}" />
</Trigger>
</Style.Triggers>
</Style>
This allows the painted color to extend across the entire row on load or when the program loses focus.
这允许绘制的颜色在加载时或当程序失去焦点时扩展到整行。
回答by 15ee8f99-57ff-4f92-890c-b56153
I found another solution, that's actually a bit simpler:
我找到了另一个解决方案,实际上更简单一点:
<DataGrid.Resources>
<!-- Select everything with orange... -->
<SolidColorBrush
Color="#ff9933"
x:Key="{x:Static SystemColors.HighlightBrushKey}" />
<!-- ...all the time -->
<SolidColorBrush
Color="#ff9933"
x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" />
</DataGrid.Resources>
The problem was that the entire row washighlighted, but when focus was lost, the "unselected" cells were highlighted with a different color: The Inactive Selection Highlight color.
问题是整行都被突出显示,但是当失去焦点时,“未选择”的单元格用不同的颜色突出显示:非活动选择突出显示颜色。
The dead area background at the end of the row is being set by your IsSelected=Truetrigger. Get rid of that, and the dead area won't "select" at all. Use three distinct colors for that, HighlightBrushKey, and InactiveSelectionHighlightBrushKey, and it all becomes clear.
行末尾的死区背景由您的IsSelected=True触发器设置。摆脱它,死区根本不会“选择”。使用三种不同的颜色,HighlightBrushKey, 和InactiveSelectionHighlightBrushKey,一切都会变得清晰。
Anton Danylov was very, very close. But the answer is not to make those brushes transparent, it's to make them the color you want them to be.
安东丹尼洛夫非常非常接近。但答案不是让这些画笔透明,而是让它们成为你想要的颜色。

