如何在ListBox中禁用高亮显示但保留选择?

40

我不知道如何防止我的ListBox突出显示所选项目。我知道我没有添加突出显示该项的触发器。

<ListBox Name="CartItems" ItemsSource="{Binding}"
         ItemTemplate="{StaticResource TicketTemplate}" 
         Grid.Row="3" Grid.ColumnSpan="9" Background="Transparent"
         BorderBrush="Transparent">
</ListBox>
8个回答

73
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <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>

@Baboon的答案在Windows Phone 8.1上不起作用,而这个可以。非常好。 - samjudson
如果您已经定义了 ListBox.ItemContainerStyle,则此答案有效。 - JwJosefy
5
我搜寻了大部分答案但都没有成功。这是唯一一个适用于我使用列表框项目模板的解决方案。 - T James

47

回答晚了,但有一个更好、更简单的解决方案:

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

这使您可以拥有一个看起来像itemscontrol的LisBox,并支持选择。

编辑:如何工作
这会更改“系统的颜色”,换句话说,您的Windows主题,仅适用于此ListBox及其子元素(我们实际上要针对ListboxItem)。

例如,通常将鼠标悬停在ListboxItem上会使其具有深蓝色的背景色,但是在这里,我们将其设置为透明(HighlightBrushKey)。

编辑(2016年6月30日):
对于最新版本的Windows,似乎这已经不够了,您还需要重新定义InactiveSelectionHighlightBrushKey

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />
感谢评论中的@packoman

1
@ClementHoang 我添加了一个解释。 - Louis Kottmann
x:Static在Windows Store应用程序中无法使用,有没有替代方法? - smohamed
2
但是,如果我将焦点设置在其他元素上,我仍然可以看到选择。:/ - Mateusz Dembski
1
@Baboon 你可能需要更新你的答案。我和Mateusz Dembski一样遇到了同样的问题,即如果ListBox没有焦点,你将看到所选项目的灰色高亮显示。解决这个问题的方法似乎是在你的答案中添加 <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" /> 这些行。虽然我还没有彻底测试它...从这里得到:https://dev59.com/SZjga4cB1Zd3GeqPJWK6 - packoman
1
即使添加了编辑,这也没有起作用。我设置了一个ItemContainerStyle,这可能导致它无法工作。 - JeremyK
显示剩余5条评论

6

完全去除高亮感觉非常奇怪,因为你不知道是否选择了任何内容,但这是一个使用WhiteSmoke(非常微妙)而不是Blue的控件模板版本。

<Window.Resources>
    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"/>
        <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="Padding" Value="2,0,0,0"/>
        <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>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="WhiteSmoke"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <ListBox HorizontalAlignment="Left" VerticalAlignment="Top" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}">
        <ListBoxItem Content="Item1"/>
        <ListBoxItem Content="Item2"/>
        <ListBoxItem Content="Item3"/>
        <ListBoxItem Content="Item4"/>
        <ListBoxItem Content="Item5"/>
        <ListBoxItem Content="Item6"/>
    </ListBox>
</Grid>

5

以下是对我有效的方法。

<Style x:Key="ListBoxNoHighlight" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Transparent"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Transparent"/>                   
            </Trigger>
        </Style.Triggers>
    </Style>

谢谢!唯一在 Windows 10 上运行并针对 .NET 6 的解决方案。 - l33t

1
在“属性”选项卡中,有一个包含两个选项的“已启用”字段:true和false。将其设置为false后,Listbox仍然保持白色且无法进行选择。刚刚才发现这个问题!

1
我在我的WP8应用程序中使用了一个技巧。我添加了一个透明的框架图像(可以看到图像的边框,就像是一幅画的框架)。滚动是可用的,任何操作事件都会触发,只是Listbox项目不再被选中。
<Grid 
        Grid.Row="0" 
        Margin="10,15"
        Background="#FF008F88">
        <ListBox 
            x:Name="listBox_content" 
            Margin="20,15"
            VirtualizingStackPanel.VirtualizationMode="Recycling">
        </ListBox>

        <!-- TV image, middle is transparent so the ListBox can be seen -->
        <Image 
                x:Name="image_tv" 
                Source="/Assets/Images/tvFrame.png" 
                Stretch="Fill"/>
        <!---->
    </Grid>

我猜这也可以使用一个全透明的图片集合设置为填充来实现。

0

您需要重新设计ListBoxItem的模板。在默认模板中,当IsSelected属性为true时,它会触发自身高亮。您只需创建一个不包含该触发器的模板即可。


这里是一个页面,列出了ListBox和ListBoxItem的模板。你可以使用它们。 - decyclone

0

我认为你需要为ListBoxItem创建一个自定义控件模板,并包含触发器以在选择该项时将背景颜色更改为“透明”。以下是如何实现此操作的示例:

<ListBox x:Name="MyListBox">
    <ListBox.Resources>
        <ControlTemplate x:Key="ListBoxItemControlTemplate" TargetType="ListBoxItem">
            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" TargetName="Bd" Value="Transparent"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template" Value="{StaticResource ListBoxItemControlTemplate}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

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