如何处理 WPF 中捕获的鼠标上的 ListBoxItem 样式触发器

发布于 2024-10-01 08:03:01 字数 429 浏览 0 评论 0原文

我有一个列表框,其中包含一些具有模板的项目。这些项目是在运行时创建的。当鼠标悬停在 ListBoxItem 上时,模板会触发缩放动画。我想在用户单击该项目时更改该项目的背景。下面的代码不起作用,因为我相信 IsMouseCaptured (单击)是由列表框选择器处理的。我有什么想法可以在 XAML 代码中完成这一切吗?

<ControlTemplate.Triggers>
        <Trigger Property="IsMouseCaptured" Value="True">
            <Setter TargetName="rec" Property="Fill" Value="Black" />
        </Trigger>    
</ControlTemplate.Triggers>

I have a listbox containing some items that have a template. The items are created at runtime. The template triggers an scale animation when the mouse is hovering over a ListBoxItem. I would like to change the background of the item when the user clicks the item. The code below does not work because I believe the IsMouseCaptured (click) is handled by the listbox selector. Any ideas how I can do this all in XAML code?

<ControlTemplate.Triggers>
        <Trigger Property="IsMouseCaptured" Value="True">
            <Setter TargetName="rec" Property="Fill" Value="Black" />
        </Trigger>    
</ControlTemplate.Triggers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-10-08 08:03:01

当用户单击列表框中的某个项目时,该项目就会被选中,因此您的触发器需要对 IsSelected-Property 进行操作。

为 ListBoxItem 创建一个样式,将触发器放在 IsSelected-Property 上,并将该样式设置为 ItemContainerStyle。

When the user clicks an item in a listbox, it is selected, therefor your trigger needs to operate on the IsSelected-Property.

Create a style for your ListBoxItem, put the Trigger on the IsSelected-Property and set that Style as ItemContainerStyle.

始终不够爱げ你 2024-10-08 08:03:01

正如 Falcon 所说,您可以使用事件来完成您的任务。这甚至可以纯粹在 XAML 中完成,如下所示:

<EventTrigger RoutedEvent="MouseDown" >
    <EventTrigger.Actions>                      
        <BeginStoryboard x:Name="ClickBackground">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames 
                    Duration="0" FillBehavior="HoldEnd"
                    Storyboard.TargetName="rec"
                    Storyboard.TargetProperty="Fill">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <SolidColorBrush Color="Black" />
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>                           
                </ObjectAnimationUsingKeyFrames>                                
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseUp" >
    <EventTrigger.Actions>                      
        <StopStoryboard BeginStoryboardName="ClickBackground" />
    </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave" >
    <EventTrigger.Actions>                      
        <StopStoryboard BeginStoryboardName="ClickBackground" />
    </EventTrigger.Actions>
</EventTrigger>

As Falcon said, you could use events for your task. This can even be done purely in XAML, like this:

<EventTrigger RoutedEvent="MouseDown" >
    <EventTrigger.Actions>                      
        <BeginStoryboard x:Name="ClickBackground">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames 
                    Duration="0" FillBehavior="HoldEnd"
                    Storyboard.TargetName="rec"
                    Storyboard.TargetProperty="Fill">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <SolidColorBrush Color="Black" />
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>                           
                </ObjectAnimationUsingKeyFrames>                                
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseUp" >
    <EventTrigger.Actions>                      
        <StopStoryboard BeginStoryboardName="ClickBackground" />
    </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave" >
    <EventTrigger.Actions>                      
        <StopStoryboard BeginStoryboardName="ClickBackground" />
    </EventTrigger.Actions>
</EventTrigger>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文