簡體   English   中英

如何設置ListBoxItem的樣式

[英]How To Style ListBoxItem

<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如沒有ListBox.SelectionMode =“None”,是否有另一種禁用選擇的方法一個列表框? WPF,XAML:如何使用ListBox ItemsSource對象的屬性綁定樣式ListBoxItem?

我期望的是沒有背景,沒有邊框,並且該項目不可調焦。

我做錯了什么?

重新定義ListBoxItemControlTemplate 下面的示例代碼使用瑞迪施彩色只是為了說明這一點,但你可以將其替換為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>

在此輸入圖像描述

並根據需要使用Transparent背景,無焦點,無突出顯示:

在此輸入圖像描述

您可以在資源中使用樣式,如:

<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中使用任何屬性,否則它將不會在樣式中描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM