我正在开发一个程序,其中我使用了一个
然而,我希望单元格中的文本居中显示,我通过以下方式实现:
然而,现在的问题是当窗口不处于活动状态时,我无法将背景设置为灰色渐变。我正在基于
GroupingGrid
(如有需要,本文末尾包含)。这个网格是基于DataGrid
构建的。我正在创建一种类似Aero的效果来匹配应用程序中的TreeView
,该树视图将被转换为分组网格(我们建错了)。当行处于活动状态时,我希望文本垂直居中,选择行的背景为蓝色渐变;当不活动时(例如窗口失去焦点),选择行的背景为灰色渐变。最初,我只在页面主题中定义了这些值:<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFD9F4FF" Offset="0"/>
<GradientStop Color="#FF9BDDFB" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="{x:Static SystemColors.ControlBrushKey}" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFEEEDED" Offset="0"/>
<GradientStop Color="#FFDDDDDD" Offset="1"/>
</LinearGradientBrush>
HighlightBrushKey
设置蓝色渐变,而ControlBrushKey
在窗口不活动时设置灰色渐变。然而,我希望单元格中的文本居中显示,我通过以下方式实现:
<controls:GroupingGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</controls:GroupingGrid.CellStyle>
然而,这会使单元格内的文本框缩小,而背景就是在这里设置的,因此它看起来有一个狭窄的蓝色带子,直接文本后面是白色的带子。为了解决这个问题,我将单元格高亮颜色设置为透明,并分配了网格行颜色,如下所示:
<Style x:Key="PrettifyRow" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>
然而,现在的问题是当窗口不处于活动状态时,我无法将背景设置为灰色渐变。我正在基于
TreeView
进行开发。<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"/>
<Condition Property="IsSelectionActive" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" Value="LightGray"/>
</MultiTrigger>
没有属性,所以我尝试使用代替。不幸的是,这并没有起作用。如何让高亮显示类似于这样?通过对此进行实验,我认为有两种方法可以使其工作 - 找到不同的方法来垂直居中文本,或者使用事件来某种方式确定选择何时处于活动状态。如果有影响的话,我不允许列自动填充 - 我手动指定要填充哪些列。
这里是承诺的:
<DataGrid x:Class="Rubberduck.Controls.GroupingGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Rubberduck.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DataGrid.Resources>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Background="WhiteSmoke" Foreground="Black" Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter></ItemsPresenter>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter></DataGridRowsPresenter>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
编辑:
我尝试通过在每个单元格中使用Grid
来设置模板。但是这种方法行格式会混乱,虽然文本居中了。