Windows8中Listbox项目选择周围有白色边框

8
我收到来自Windows8用户的投诉,说在ListBoxes上的SelectedItem周围出现了奇怪的框架。在Windows7中不存在这个问题,迄今为止我没有找到任何方法来摆脱这个白色边框。据我所知,Windows8现在使用ControlBrushKey而不是HighlightBrushKey,但将其设置为Transparent没有影响。目前我没有Windows8开发环境,所以我尝试的所有修复都是纯猜测。ListBox资源:
 <ListBox.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
     <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />

     <Style TargetType="ListBoxItem">
         <Setter Property="FocusVisualStyle" Value="{x:Null}" />
         <Setter Property="BorderBrush" Value="Transparent" />
     </Style>
 </ListBox.Resources>

整个Xaml文件可以在此处找到:https://github.com/saddam213/MPDisplay/blob/master/GUIFramework/GUI/Controls/GUIList.xaml

框架图片:(选中区域周围的白色方框)

enter image description here

如果有人知道如何去除它,那就太好了。


尝试一下这个,看看是否有帮助:http://stackoverflow.com/questions/7255416/setting-systemcolors-overrides-in-an-implicit-style - eemwingg
请附上已编译的.EXE文件,以便进行测试和调试。我会告诉您如何禁用它。 - Erti-Chris Eelmaa
@ss_ddam213,链接的XAML文件是正确的吗?因为StackPanelOrientation属性被设置为Horizontal。但是你的屏幕显示的却不是这样。 - Suresh
@sthotakura,它是可配置的,有三个视图:水平、垂直和 Coverflow,它设置在 VirtualizingStackpanels 触发器中,所有视图都得到相同的矩形。我想我只需安装一个 Win8 虚拟机并尝试找到问题。 - sa_ddam213
请上传完整的项目,以便我们测试您的问题。 - dev hedgehog
显示剩余10条评论
3个回答

2
在你的原始帖子的评论中,你说:
“我不会仅因为需要覆盖刷子而重建控件,如果我需要覆盖整个ListBox模板以删除选择颜色,我就不会支持Windows8,一旦我安装Win8,使用Snoop查找刷子将很简单。”
然而,“重建” ListBoxItem 并不是非常困难。实际上,这可能比强制使用刷子更简单,因为您不需要担心在 Windows 版本之间覆盖每个 UX 更改。我正在构建的一个特定应用程序要求它运行在从 XP 到 8.1 的每个操作系统上;通过自定义所有内容直至窗口边框,我实现了在所有操作系统上的统一外观。
你最好的选择是通过创建模板来样式化 ListBoxItem 的每个方面,类似于以下内容:
<Style TargetType="ListBoxItem">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Blue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

显然,您需要修改样式以获得所需的确切行为。

我知道覆盖模板很简单,但我觉得很沮丧的是,为了满足一个操作系统,我必须这样做,而在之前的所有版本中,只需要一行XAML就可以解决问题。 - sa_ddam213
@sa_ddam213:现在可能只是单个操作系统,但在未来的 Windows 版本中,您可能会遇到更多问题。最好第一次就把它做对。 - Ming Slogar
这应该可以工作。除非我确保borderBrush设置为透明,并添加<Setter Property="OverridesDefaultStyle" Value="True"/>。 - Kapitán Mlíko
@ViktorLaCroix:我只是在告诉OP如何使用模板。说实话,我从来没有发现过<Setter Property="OverridesDefaultStyle" Value="True" />的用途;我的样式似乎没有那行也能正常工作。 - Ming Slogar

1

是的,上一个答案确实有帮助。 这就是我如何消除ListBoxItem周围的白色框架:

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Style.Setters>
                    <Setter Property="BorderBrush" Value="Gray"/>
                    <Setter Property="BorderThickness" Value="0,0,0,1"/>
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Background" Value="Black"/>
                </Style.Setters>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Background="Black"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

1
我认为这可能对您有所帮助。使用Trigger属性来判断是否选择。
 <ListBox Name="lst">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True" >
                    <Setter Property="BorderBrush" Value="Wheat"/>
                    <Setter Property="BorderThickness" Value="0"/>
                  </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>

</ListBox>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接