【问题标题】:Overriding SystemColors in local Resources dictionary覆盖本地资源字典中的 SystemColors
【发布时间】:2016-10-28 18:18:09
【问题描述】:

我正在尝试隐藏指示 WPF ListBox 中选择的视觉提示。 This answer 建议这应该通过覆盖 SystemColorsListBox 来工作。

我创建了一个新的 WPF 项目并像这样编辑了MainWindow.xaml

<Window x:Class="WpfListboxWithoutSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfListboxWithoutSelection"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="325">
  <Grid>
    <ListBox>
      <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
      </ListBox.Resources>
      <s:String>Item 1</s:String>
      <s:String>Item 2</s:String>
      <s:String>Item 3</s:String>
    </ListBox>
  </Grid>
</Window>

不幸的是这不起作用,窗口显示如下:

知道我做错了什么吗?如何删除显示在所选项目和悬停项目上的蓝色?

【问题讨论】:

    标签: wpf xaml visual-studio-2015 listbox systemcolors


    【解决方案1】:

    如果您希望在选择列表框项或鼠标悬停时禁用突出显示,您可以使用以下代码。

    <Style TargetType="ListBoxItem" x:Key="ListBoxItemStyle">
        <Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"/>
    

    【讨论】:

      【解决方案2】:

      肯定不行。 由于 ListBox.Resources 将为 ListBox 而不是为 ListBoxItem 设置资源。 所以这里有一个解决方案:

      <Style TargetType="{x:Type ListBoxItem}">
          <Setter Property="FocusVisualStyle" Value="{x:Null}" />
          <Style.Resources>
              <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
              <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
          </Style.Resources>
      </Style>
      

      将此样式附加到 windows.resource 中,如:

      <Window.Resources>
          <Style TargetType="{x:Type ListBoxItem}">
              <Setter Property="FocusVisualStyle" Value="{x:Null}" />
              <Style.Resources>
                  <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                  <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
              </Style.Resources>
          </Style>
      </Window.Resources>
      

      而且你可以将更多的 SolidColorBrush 放入 Style.Resources 中,就像你在代码中所做的那样。

      【讨论】:

      • 但是为什么the other answer 有 37 个赞,而且每个人都说它对他们有用?
      • 你的解决方案也是正确的,但有时它取决于机器,甚至你的解决方案也在我的机器上运行。我想我的代码可以在你的机器上运行(它看起来像 windows 8.1 或 10)。
      【解决方案3】:

      我发现覆盖此默认行为的唯一方法是覆盖 ListBoxItem 的默认样式。我从 blend 中提取默认样式并根据您的需要覆盖它。以下是我的做法:

      <Window
          x:Class="WpfApplication2.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:local="clr-namespace:WpfApplication2"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:s="clr-namespace:System;assembly=mscorlib"
          Title="MainWindow"
          Width="525"
          Height="350"
          mc:Ignorable="d">
      
          <Window.Resources>
      
              <!--  This is default ListboxItemStyle  -->
              <Style x:Key="ListboxItemControlDefault" TargetType="{x:Type ListBoxItem}">
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="{x:Type ListBoxItem}">
                              <Border
                                  x:Name="Bd"
                                  Padding="{TemplateBinding Padding}"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  SnapsToDevicePixels="True">
                                  <ContentPresenter
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Content="{TemplateBinding Content}"
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                              </Border>
                              <ControlTemplate.Triggers>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="IsMouseOver" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="#1F26A0DA" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="#A826A0DA" />
                                  </MultiTrigger>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="Selector.IsSelectionActive" Value="False" />
                                          <Condition Property="IsSelected" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="#3DDADADA" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="#FFDADADA" />
                                  </MultiTrigger>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="Selector.IsSelectionActive" Value="True" />
                                          <Condition Property="IsSelected" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="#3D26A0DA" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="#FF26A0DA" />
                                  </MultiTrigger>
                                  <Trigger Property="IsEnabled" Value="False">
                                      <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                  </Trigger>
                              </ControlTemplate.Triggers>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
      
      
              <!--  This is modified ListboxItemStyle  -->
              <Style x:Key="ListboxItemControlModified" TargetType="{x:Type ListBoxItem}">
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="{x:Type ListBoxItem}">
                              <Border
                                  x:Name="Bd"
                                  Padding="{TemplateBinding Padding}"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  SnapsToDevicePixels="True">
                                  <ContentPresenter
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Content="{TemplateBinding Content}"
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                              </Border>
                              <ControlTemplate.Triggers>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="IsMouseOver" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                                  </MultiTrigger>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="Selector.IsSelectionActive" Value="False" />
                                          <Condition Property="IsSelected" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                                  </MultiTrigger>
                                  <MultiTrigger>
                                      <MultiTrigger.Conditions>
                                          <Condition Property="Selector.IsSelectionActive" Value="True" />
                                          <Condition Property="IsSelected" Value="True" />
                                      </MultiTrigger.Conditions>
                                      <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                                      <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                                  </MultiTrigger>
                                  <Trigger Property="IsEnabled" Value="False">
                                      <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                  </Trigger>
                              </ControlTemplate.Triggers>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </Window.Resources>
      
          <Grid>
              <ListBox ItemContainerStyle="{StaticResource ListboxItemControlModified}" SelectionChanged="ListBox_SelectionChanged">
                  <s:String>Item 1</s:String>
                  <s:String>Item 2</s:String>
                  <s:String>Item 3</s:String>
                  <s:String>Item 4</s:String>
              </ListBox>
          </Grid>
      </Window>
      

      你仍然可以选择项目:

      private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var listBox = sender as ListBox;
      
          var selectedItem = listBox.SelectedItem;
      }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        相关资源
        最近更新 更多