如何禁用列表框上的突出显示但保持选择?

时间:2010-12-03 08:44:07

标签: wpf xaml listbox

我无法找到如何不允许我的ListBox突出显示所选项目。我知道我没有添加触发器来突出显示该项目。

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

7 个答案:

答案 0 :(得分:54)

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

答案 1 :(得分:45)

迟到的答案,但有一个更好,更简单的解决方案:

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

这允许你有一个看起来像项目控件的LisBox,但是支持选择。

修改: 如何运作
这改变了“系统的颜色”,换句话说就是你的windows主题,仅适用于这个ListBox及其子代(我们实际上想要定位ListboxItem)。

例如,悬停ListboxItem通常会为其提供深蓝色背景,但在此我们将其设置为透明(HighlightBrushKey)。

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

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

感谢评论中的@packoman

答案 2 :(得分:5)

完全删除突出显示非常奇怪,因为您不知道您是否选择了任何内容,但这里是使用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>

答案 3 :(得分:1)

在“属性”选项卡中,有一个“启用字段”,其中包含2个选项,true和false。通过将其设置为false,列表框保持白色并且选择不可用。刚想出来了!

答案 4 :(得分:1)

我在谈论我在WP8应用程序中所做的一个技巧。

我在它上方添加了一个透明的框架图像(图像的边框被看到,想象它像一个相框)。滚动功能正常,任何操作事件都只是因为没有选择列表框项目而触发。

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

我想这可以使用完全透明的图像设置为Fill。

答案 5 :(得分:1)

这对我有用。

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

答案 6 :(得分:0)

您必须重新模板ListBoxItem。在默认模板中,它有一个触发器,在IsSelected属性为true时突出显示。您只需要创建一个没有该触发器的模板。