【问题标题】:How To Style ListBoxItem如何设置 ListBoxItem 的样式
【发布时间】:2016-07-01 14:39:05
【问题描述】:
<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?

我期望的是没有背景,没有边框,并且项目不可聚焦。

我做错了什么?

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    重新定义ListBoxItemControlTemplate。下面的示例代码使用 redish 颜色只是为了说明这一点,但您可以将其替换为 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 背景,无焦点,无突出显示:

    【讨论】:

      【解决方案2】:

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

      <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中使用样式中使用的任何属性,否则它将不会表现出样式中描述的行为。

      【讨论】:

        猜你喜欢
        • 2012-07-12
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 2010-10-29
        • 2014-10-21
        • 2011-11-12
        • 1970-01-01
        • 2012-09-20
        相关资源
        最近更新 更多