gpt4 book ai didi

c# - WPF 数据网格 : How To Keep Selected Rows Highlighted?

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:43 25 4
gpt4 key购买 nike

我的 WPF 项目中的同一个窗口上有多个 DataGrid 控件。当我单击网格中的一行时,它会突出显示。当我单击另一个网格中的一行时,该行突出显示并且第一个网格中的行变得非常微弱突出。我如何设计这个窗口,以便每个网格可以同时突出显示选定的行?我的项目是在 .NET 4.0 中构建的,因此 InactiveSelectionHighlightBrushKey 似乎不起作用。我正在使用下面的代码,它基本上可以工作,除非当我单击不同的网格时,前一个网格将行文本颜色更改为黑色而不是白色。我尝试将 ControlTextBrushKey 设置为白色,但这使得网格中的每一行都变成白色,这意味着未选中的行因为不可见,因为背景也是白色的。是否有更优雅的方法来实现此目的,即创建用户控件或继承 DataGrid 类,因为我需要在项目中多次插入此代码。

                <DataGrid Name="dgStores" >
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{x:Static Colors.DodgerBlue}"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static Colors.DodgerBlue}"/>
</DataGrid.Resources>
</DataGrid>

最佳答案

使用 Style.Triggers 为 DataGridCell 设置 IsSelected 颜色。

<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Static SystemColors.HighlightBrush}"></Setter>
<Setter Property="Foreground" Value="{x:Static SystemColors.HighlightTextBrush}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

关于c# - WPF 数据网格 : How To Keep Selected Rows Highlighted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31880898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com