如何让 ListBoxItem.IsMouseCaptured 触发样式更改?

How do you get ListBoxItem.IsMouseCaptured to trigger style changes?

我正在使用以下样式:

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Foreground" Value="{DynamicResource Button-Foreground}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <TextBlock Padding="10"
                            Background="{TemplateBinding Background}"
                            Foreground="{TemplateBinding Foreground}">
                    <ContentPresenter/>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{DynamicResource Button-Foreground-Disabled}"/>
        </Trigger>
        <Trigger Property="IsMouseCaptured" Value="True">
            <Setter Property="Background" Value="{DynamicResource Button-Background-Hover}"/>
            <Setter Property="Foreground" Value="{DynamicResource Button-Foreground}"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True"/>
                <Condition Property="IsMouseCaptured" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="{DynamicResource Button-Background-Hover}"/>
            <Setter Property="Foreground" Value="{DynamicResource Button-Foreground-Hover}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="{DynamicResource Button-Foreground}"/>
            <Setter Property="Foreground" Value="{DynamicResource Button-Foreground-Hover}"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>

我遇到的问题是 IsMouseCaptured 触发器从未发生。它保持 IsMouseOver 风格,即使我点击它(背景是故意相同的颜色,但我尝试改变只是为了实验,它根本没有改变,前景当然也没有改变)。

有没有办法在单击特定列表框项目时触发它?所有其他触发器都按预期工作。

Is there a way to have triggers occur for a specific list box item when it is clicked?

不使用任何内置 属性。

您可以创建自己的自定义 ListBoxItem,添加可以绑定到的 IsPressed 属性:

public class CustomListBoxItem  : ListBoxItem
{
    public static readonly DependencyProperty IsPressedProperty = DependencyProperty.Register(
        nameof(IsPressed), typeof(bool), typeof(CustomListBoxItem), new PropertyMetadata(false));

    public bool IsPressed
    {
        get => (bool)GetValue(IsPressedProperty);
        set => SetValue(IsPressedProperty, value);

    }
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        IsPressed = true;
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        IsPressed = false;
    }
}

除非您明确创建 ListBoxItem 元素,否则您还需要一个自定义 ListBox 来生成自定义类型的容器:

public class CustomListBox : ListBox
{
    protected override DependencyObject GetContainerForItemOverride() =>
        new CustomListBoxItem();
}