Triggers WPF:ListBoxItem背景未显示

Triggers WPF:ListBoxItem背景未显示,triggers,styles,Triggers,Styles,我有一个关于ListBoxItem样式和触发器的问题。 我创建了一个测试项目。在列表中我有一些项目。我为这个列表框创建了样式和触发器。当我将鼠标悬停到项IsMouseOver时,会触发works,并将边距、字体大小、光标、前景(而不是背景)和TesxDecorations设置为下划线。下面是测试项目代码 XAML <Window x:Class="TestForJamshed.MainWindow" xmlns="http://schemas.microsoft.com/winfx

我有一个关于ListBoxItem样式和触发器的问题。 我创建了一个测试项目。在列表中我有一些项目。我为这个列表框创建了样式和触发器。当我将鼠标悬停到项IsMouseOver时,会触发works,并将边距、字体大小、光标、前景(而不是背景)和TesxDecorations设置为下划线。下面是测试项目代码

XAML

<Window x:Class="TestForJamshed.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="ListBox" x:Key="PanelPreviewShortListBox">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                        <ItemsPresenter Margin="-10 0 0 0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Margin" Value="10 0 0 0"/>
                    <Setter Property="BorderThickness" Value="0"/>

                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Margin" Value="-10 0 0 0"/>
                            <Setter Property="TextBlock.FontSize" Value="15"/>
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Margin" Value="-10 0 0 0"/>
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="TextBlock.FontSize" Value="15"/> 
                            <Setter Property="Cursor" Value="Hand"/>
                            <Setter Property="Foreground" Value="#0000FF"/>
                            <Setter Property="TextBlock.TextDecorations" Value="Underline"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <ListBox Name="uiListBox" HorizontalAlignment="Right" Style="{DynamicResource PanelPreviewShortListBox}" Width="200" Background="LightGreen"/>
    </StackPanel>
</Grid>

代码

 List<string> list = new List<string>();
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");
        list.Add("This is an item.");

        uiListBox.ItemsSource = list;
List List=新列表();
添加(“这是一个项目”);
添加(“这是一个项目”);
添加(“这是一个项目”);
添加(“这是一个项目”);
添加(“这是一个项目”);
添加(“这是一个项目”);
添加(“这是一个项目”);
uiListBox.ItemsSource=列表;

塔克斯

TargetType
ListBox
,我把它改成了
ListBoxItem
,它就热了