【问题标题】:Removing WPF ListView/GridView highlight chrome删除 WPF ListView/GridView 高亮铬
【发布时间】:2010-11-18 03:10:48
【问题描述】:

我有带有 GridView 视图的 WPF ListView,我想删除行突出显示的任何痕迹。

可以在本网站的一个答案中找到这段有用的代码:

     <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
           <Setter Property="Control.Focusable" Value="False"/>
           <Style.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                 <Setter Property="Background" Value="{x:Null}" />
                 <Setter Property="BorderBrush" Value="{x:Null}" />
              </Trigger>
           </Style.Triggers>
        </Style>
     </ListView.ItemContainerStyle>

但是,虽然此更改有助于删除大部分突出显示,但它不会删除当鼠标移到 ListView 行上时仍然出现的水平条。如何删除它?

我已经处理了涉及 Button 的类似问题,并找到了一个通过删除其 chrome 来更改 Button 模板的解决方案。

在这种 ListView/GridView 的情况下,我无法找到相应的 chrome 和要更改的模板。

【问题讨论】:

    标签: wpf listview wpf-controls highlight


    【解决方案1】:

    使用您的代码,我根本看不到任何行。你现在的默认主题是什么?露娜、航空等?可能是您的与我的不同,因此铬有所不同。您的ListView 上还有其他特殊设置吗?

    Style Snooper 或Show Me The Template 可能会帮助您找到负责您所看到的线条的视觉元素。你可能也对re-templating你的ListView感兴趣以获得你想要的效果。

    【讨论】:

    • 它是Vista主题,我相信它是Aero,因为我有玻璃效果。事实上,使用 XP 的人看不到高亮栏。
    【解决方案2】:

    我现在不在 Windows PC 前进行测试,但我遇到了与列表框类似的问题,我通过将以下内容放入我的 Window.Resources 来解决此问题

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    

    不确定它是否适用于您的列表视图。

    【讨论】:

    • 一些博客和回答都指出了这种方法,但由于某种原因,它对我没有用。
    • 谢谢!当我使用 Style targetType="ListViewItem" 将它放在我的窗口资源中时,这对我有用。我还添加了一行将 SystemColors.HighlightTextBrushKey 设置为 SystemColors.WindowTextColor 否则以防止我的文本颜色切换到背景颜色(从而消失)。
    • 对我来说,它适用于没有 Gridview 的 ListView。但不适用于 Gridview。
    【解决方案3】:

    如果您安装了 Windows SDK,您可以在以下位置找到所有默认样式的 XAML 源代码(假设您安装了示例):

    %ProgramFiles%\微软 SDKs\Windows\v6.1\Samples\WPFSamples.zip

    zip 文件包含一个文件夹 Core,其中包含 AeroTheme、LunaTheme 等,其中包含默认样式的源代码。不幸的是,这些文件非常大(Aero 大约 8500 行)并且结构或格式不是很好 (IMO)。

    ListViewItem 的默认控件模板如下所示:

    <ControlTemplate TargetType="{x:Type ListViewItem}">
      <Border CornerRadius="2" SnapsToDevicePixels="True"
              BorderThickness="{TemplateBinding BorderThickness}" 
              BorderBrush="{TemplateBinding BorderBrush}" 
              Background="{TemplateBinding Background}">
        <Border Name="InnerBorder" CornerRadius="1" BorderThickness="1">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition MaxHeight="11" />
              <RowDefinition />
            </Grid.RowDefinitions>
    
            <Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
            <GridViewRowPresenter Grid.RowSpan="2" 
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
          </Grid>
        </Border>
      </Border>
    
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background" Value="{StaticResource ListItemHoverFill}" />
          <Setter Property="BorderBrush" Value="#FFCCF0FF" />
          <Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
        </Trigger>
    
        <Trigger Property="IsSelected" Value="True">
          <Setter Property="Background" Value="{StaticResource ListItemSelectedFill}" />
          <Setter Property="BorderBrush" Value="#FF98DDFB" />
          <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#80FFFFFF" />
          <Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
          <Setter TargetName="UpperHighlight" Property="Fill" Value="#40FFFFFF" />
        </Trigger>
    
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="True" />
            <Condition Property="Selector.IsSelectionActive" Value="False" />
          </MultiTrigger.Conditions>
    
          <Setter Property="Background" Value="{StaticResource ListItemSelectedInactiveFill}" />
          <Setter Property="BorderBrush" Value="#FFCFCFCF" />
        </MultiTrigger>
    
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="True" />
            <Condition Property="IsMouseOver" Value="True" />
          </MultiTrigger.Conditions>
    
          <Setter Property="Background" Value="{StaticResource ListItemSelectedHoverFill}" />
          <Setter Property="BorderBrush" Value="#FF98DDFB" />
        </MultiTrigger>
    
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
    

    删除所有突出显示的最佳选择可能是将 ControlTemplate 替换为您自己的,其中仅包含 GridViewRowPresenter(可能在单个边框中)。不要忘记包含在禁用控件时使项目变灰的触发器。

    【讨论】:

    • 高光一定是它!我在这里查看模板:msdn.microsoft.com/en-us/library/ms788717.aspx,由于它必须用于不同的主题,我无法解释该栏。
    • 我可以确认 UpperHighlight 矩形是罪魁祸首。谢谢!
    • 太棒了...只有在 WPF 中我才必须完全覆盖样式以摆脱单个突出显示元素。
    猜你喜欢
    • 2011-08-30
    • 2018-06-04
    • 2011-10-06
    • 2016-08-16
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多