如何设置ListBoxItem的样式

3
<Border Grid.Row="1" Padding="15" Margin="15" BorderBrush="LightBlue" Background="AliceBlue" BorderThickness="1">
        <ListBox ItemsSource="{Binding Configs}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Focusable" Value="False" />
                    <Setter Property="BorderThickness" Value="0" />
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <view:Config />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Border>

这是我在XAML中编写的代码。然而,它似乎没有影响任何内容。
SO上有很多类似问题的帖子,但是所有的回复都是“使用 ListBox.ItemContainerStyle”,这是我已经做到了:S,例如There is no ListBox.SelectionMode="None", is there another way to disable selection in a listbox?WPF, XAML: How to style a ListBoxItem using binding on property of ListBox ItemsSource object? 我期望的是没有背景、没有边框并且该项不可聚焦。
我做错了什么?
2个回答

3
重新定义ListBoxItem的ControlTemplate。下面的示例代码使用红色来突出重点,但您可以将其替换为透明:Transparent
<Window x:Class="WpfApplication235.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:WpfApplication235"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

    <x:Array x:Key="SampleData" Type="{x:Type sys:String}">
        <sys:String>Item 1</sys:String>
        <sys:String>Item 2</sys:String>
        <sys:String>Item 3</sys:String>
        <sys:String>Item 4</sys:String>
        <sys:String>Item 5</sys:String>
    </x:Array>

    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <TextBlock Text="{Binding}">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="#1FFF0000"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

<Grid>

    <ListBox x:Name="listBox" ItemsSource="{StaticResource SampleData}" 
             ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
             Height="150" Margin="0" Width="250"/>

</Grid>

要求使用透明背景、无焦点、无高亮的方式,如下图所示:

enter image description here

enter image description here


1
您可以在资源中使用样式,例如:

<Window.Resources>

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="#D8D8D8"/>
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="FontWeight" Value="400"/>
        <Setter Property="Height" Value="30"/>
        <!--and so on whatever you want...-->
    </Style>

</Window.Resources>

并删除类似以下代码:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="BorderThickness" Value="0" />
    </Style>
</ListBox.ItemContainerStyle>

不要在listBox中使用与样式中使用的属性相同的属性,否则它将无法按照样式中所描述的行为。


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