在Windows 8上的列表框项目选择周围的白框

时间:2013-11-27 02:56:08

标签: c# wpf windows-8 listbox

我收到了Windows8个用户对SelectedItemListBoxes周围奇怪框架的投诉

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

如果有人知道如何摆脱这种情况就会很棒。

3 个答案:

答案 0 :(得分: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>

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

答案 1 :(得分:1)

我认为这可能对您有所帮助。使用isSelected的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>

答案 2 :(得分: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>
相关问题