WPF:ListBoxItem后台未成功



我有一个关于ListBoxItem样式和触发器的问题。我创建了一个测试项目。清单上有一些项目。我为这个ListBox创建了样式和触发器。当我将鼠标悬停到项目时,IsMouseOver触发器工作,并将边距、字体大小、光标、前景设置为下划线,但不将背景和TesxDecoresions设置为下划线。这是测试项目的代码。

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;

谢谢!

TargetTypeListBox,我把它改成了ListBoxItem,它很好!

相关内容

  • 没有找到相关文章

最新更新