我有一个数据网格,我正在进行文本搜索。当搜索确定应该突出显示该行时,我在底层对象中为布尔值赋值为“true”,在我的WPF xml中,我有以下内容:
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="LightGray"/>
</Style>
</DataGrid.Style>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Highlighted, Mode=TwoWay}" Value="True">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
注意“Binding Highlited”;这就是行突出显示的方式。在数据网格中选择行时,这似乎工作正常。发生这种情况时,当数据网格失去焦点时,所选行将变为灰色。当数据网格失去焦点时,如何指定SELECTED ROW的行颜色(意思是“突出显示”为真),这样仍然显示黄色(可能更暗,因此很明显它已被选中)?
答案 0 :(得分:1)
试试这个:
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="LightGray"/>
</Style>
</DataGrid.Style>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Highlight}" Value="True">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF3CF1C8" />
<Setter Property="BorderBrush" Value="#FF3CF1C8" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
<Condition Binding="{Binding Highlight}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFFBAE8A" />
<Setter Property="BorderBrush" Value="#FFFBAE8A" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF3CF1C8" />
<Setter Property="BorderBrush" Value="#FF3CF1C8" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="True"/>
<Condition Binding="{Binding Highlight}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFFBAE8A" />
<Setter Property="BorderBrush" Value="#FFFBAE8A" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
它保持所选行的选择,以防DataGrid失去焦点,并且突出显示的行会获得不同的选择颜色,这也会在丢失焦点时保留。