首先,这与将 WPF ListBox 的非活动突出显示颜色设置为活动突出显示颜色不同。下面给出了对此的解释。
环境:
我在 UserControl 中有一个 WPF ListBox,稍后将其放入使用重主题的应用程序中。从UserControl的角度来看,我事先并不知道主题会是什么样的。
期望的行为:
如果 ListBox 在某些时候没有焦点,我仍然希望选定的 ListBoxItems 具有与 ListBox 具有焦点相同的外观。
附加信息:
请注意,仅将颜色设置为某些系统默认值是行不通的。这样做会覆盖包含应用程序的主题。(这就是为什么这个问题不是上面链接问题的重复的原因。)
有没有办法实现这一点,例如使用XAML?
编辑:经过一番研究,我想我想创建“默认”样式的副本ListBoxItem(“默认”至少在 UserControl 级别的默认情况下),其中所有Triggers with都Property="Button.IsFocused" Value="False"不会被触发并且所有的TriggersProperty="Button.IsFocused" Value="True"都会被触发。
不幸的是,我不知道从哪里开始研究如何实现这一目标。因此,任何有关我可以开始研究的地方的提示也将不胜感激。
似乎您想要实现将聚焦样式设置为与非聚焦样式相同,而无需编辑主题并以独立于主题的方式进行操作。据我所知,这是不可能的,主要是因为每个主题在技术上都可以ListBoxItem以不同的方式实现焦点行为。事实上,我见过一个主题,其中您所需的行为是 ListBoxItem 的行为!
现在,如果您愿意修改每个主题以满足您的需求,请继续阅读。
如果您要全局修改主题,则可以直接编辑样式ListBoxItem(在找到它存在的位置之后)。如果您希望更具体地应用更改,那么您最终将复制当前ListBoxItem样式(从您正在编辑的任何主题)并对其进行更改。
默认 ListBoxItem 主题的副本如下(我使用 Visual Studio 制作副本)。对于每个主题,您需要更改的内容会略有不同,但总体思路是相同的。
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Padding" Value="4,1"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
关键部分在中间:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
</MultiTrigger>
Run Code Online (Sandbox Code Playgroud)
这是为所选项目设置聚焦和未聚焦两种不同的样式。
为了获得您想要的行为,您有以下两种选择之一:您可以简单地将其变成一个简单的触发器,将IsSelected上面的块替换为:
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)
或者您可以更改Item.SelectedInactive.Background和Item.SelectedInactive.Border属性以匹配活动颜色(这是在列表框样式之上):
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FF26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
Run Code Online (Sandbox Code Playgroud)
一般来说,第一种方法是首选,因为它更清楚发生了什么。
现在,上面的默认主题副本ListBoxItem将更改所有ListBoxItem主题。如果您只想更改一些,那么您需要向“复制的样式”添加一个键,如下所示:
<Style x:Key="InactiveLikeActive" TargetType="{x:Type ListBoxItem}">
Run Code Online (Sandbox Code Playgroud)
然后在您想要应用样式的上方某个级别(甚至可能只是一个 ListBox 本身),添加以下样式定义:
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource InactiveLikeActive}" />
Run Code Online (Sandbox Code Playgroud)
例如:
<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource InactiveLikeActive}" />
</ListBox.Resources>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
虽然 WPF 可以覆盖几乎所有默认外观,但它并不一定会使这件事变得容易或简单。
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |