【问题标题】:In a WPF ListBox, how to "copy" the focused selection style for items over to the non-focused selection style?在 WPF 列表框中,如何将项目的焦点选择样式“复制”到非焦点选择样式?
【发布时间】:2016-03-09 11:58:17
【问题描述】:

首先,这不是与Setting the Inactive Highlight Colour of a WPF ListBox to the Active Highlight Colour 的重复。对此的解释如下。

设置:

我在 UserControl 中有一个 WPF 列表框,稍后将把它放入使用大量主题的应用程序中。从UserControl的角度来看,我事先并不知道主题化会是什么样子。

期望的行为:

如果 ListBox 在某些时候没有焦点,我仍然希望选定的 ListBoxItems 具有与 ListBox 确实有焦点相同的外观。

其他信息:

请注意,仅将颜色设置为某些系统默认值将不起作用。这样做会覆盖包含应用程序的主题。 (这就是为什么这个问题不是上面链接问题的重复。)

有没有办法实现这一点,例如使用 XAML?

编辑:经过一番研究,我想我想创建一个“默认”ListBoxItem 样式的副本(“默认”至少就级别的默认而言) UserControl 的),其中所有带有Property="Button.IsFocused" Value="False" 的Triggers 将不会被触发,而所有带有Property="Button.IsFocused" Value="True" 的Triggers 将始终被触发。

不幸的是,我什至不知道从哪里开始研究如何实现这一目标。因此,对于我可以开始研究的地方的任何提示也将不胜感激。

【问题讨论】:

  • 您尝试使用的主题是您自己的主题(由您控制​​)还是第 3 方主题?
  • 很抱歉没有尽快回复您。回想起来,我不应该在假期前直接开始赏金。 ;) 这个用户控件背后的想法是应该可以将它放入任何类型的应用程序中。然而,在实践中,它很可能只在一个应用程序中使用——我可以在其中修改主题。显然,我对独立于主题的答案更感兴趣......但由于似乎没有一种简单的方法来实现这样的事情(按照你的答案),我想一种更专业的方法(比如你的) 也是一个解决方案。

标签: wpf xaml listbox


【解决方案1】:

总结

您似乎想要实现将焦点样式设置为等于非焦点样式,而无需编辑主题并以独立于主题的方式进行。据我所知,这是不可能的,主要是因为每个主题在技术上都可以以不同的方式实现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>

关键部分在中间:

<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>

这是为选定的项目设置焦点和非焦点时的两种不同样式。

要获得您想要的行为,您有两种选择之一;您可以简单地将其变成IsSelected 上的简单触发器just,将上面的块替换为:

<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>

或您可以更改 Item.SelectedInactive.Background 和 Item.SelectedInactive.Border 属性以匹配活动颜色(这在 ListBox 样式之上):

<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"/>

通常首选第一种方法,因为它更清楚发生了什么。

其他约束

现在,默认主题ListBoxItem 的上述副本将为所有ListBoxItems 更改它。如果您只想更改一些,那么您需要在“复制样式”中添加一个键,如下所示:

<Style x:Key="InactiveLikeActive" TargetType="{x:Type ListBoxItem}">

然后在您希望应用样式的上方某个级别(甚至可能只是一个 ListBox 本身),添加以下样式定义:

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource InactiveLikeActive}" />

例如:

<ListBox>
  <ListBox.Resources>
    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource InactiveLikeActive}" />
  </ListBox.Resources>
  <ListBoxItem>One</ListBoxItem>
  <ListBoxItem>Two</ListBoxItem>
</ListBox>

结束思考

虽然 WPF 可以覆盖几乎所有的默认外观,但它并不一定会让它变得容易或简单。

【讨论】:

  • 明天晚上我会仔细看看这个!非常感谢。
  • 我为您的回答获得了赏金。我会等到放假后再尝试接受它。
【解决方案2】:

已实现的最短变体

<ListBox ItemsSource="{Binding ElectrEquipAll}"
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" >
                        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="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>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Background"  Value="#1F26A0DA"/>
                                            <Setter Property="BorderBrush"  Value="#a826A0Da"/>
                                        </Trigger>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter Property="Background"  Value="#3D26A0DA"/>
                                            <Setter Property="BorderBrush"  Value="#FF26A0DA"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

【讨论】:

  • 这很好,但可以多解释一下。
猜你喜欢
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 2010-10-10
  • 2018-03-04
  • 1970-01-01
相关资源
最近更新 更多