WPF,XAML。ListBoxItem触发器不影响整个选择?

3

以下是我的问题:

<Style x:Key="listbox_minecraft_container_style" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background" Value="{DynamicResource image_listbox_item_background}"/>
    <Setter Property="Margin" Value="1"/>
    <Setter Property="BorderBrush" Value="#FF8B8B8B"/>
    <Setter Property="BorderThickness" Value="0.4"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="Width" Value="180"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{DynamicResource image_listbox_item_background_fulopacity}"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="#FF42A855"/>
        </Trigger>
    </Style.Triggers>
</Style>

如果我现在点击一个ListBoxItem,它的背景属性不会被正确地设置。它会设置新的Foreground和新的FontWeight,但不会设置Background
这是我在MainWindow中的ListBox:
<ListBox x:Name="listBox_vanilla_versions"
         Style="{StaticResource listbox_minecraft}" 
         ItemContainerStyle="{StaticResource listbox_minecraft_container_style}" 
         Margin="10,122,484.592,198.5">
    <ListBox.Items>
        <ListBoxItem Content="hi"/>
    </ListBox.Items>
</ListBox>

1
如果不使用动态资源,将其设置为“红色”是否会发生同样的情况? - ΩmegaMan
这也不会改变背景。 - DaNeubi
1个回答

3
这是因为默认的控件模板。您需要覆盖它:
<Style x:Key="listbox_minecraft_container_style" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
        <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
        <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
        <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>
        <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
        <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
    </Style.Resources>
    <Setter Property="Background" Value="{DynamicResource image_listbox_item_background}"/>
    <Setter Property="Margin" Value="1"/>
    <Setter Property="BorderBrush" Value="#FF8B8B8B"/>
    <Setter Property="BorderThickness" Value="0.4"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="Width" Value="180"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{DynamicResource image_listbox_item_background_fulopacity}"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="#FF42A855"/>
    </Trigger>
    </Style.Triggers>
</Style>

谢谢。我现在需要在每个对象上覆盖默认模板吗? - DaNeubi
你是指每个元素类型都要吗?不是的,但这取决于你想做什么以及默认模板如何定义 :) - mm8
好的,非常感谢。 :) - DaNeubi

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