如何处理 WPF 中捕获的鼠标上的 ListBoxItem 样式触发器
我有一个列表框,其中包含一些具有模板的项目。这些项目是在运行时创建的。当鼠标悬停在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用户单击列表框中的某个项目时,该项目就会被选中,因此您的触发器需要对 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.
正如 Falcon 所说,您可以使用事件来完成您的任务。这甚至可以纯粹在 XAML 中完成,如下所示:
As Falcon said, you could use events for your task. This can even be done purely in XAML, like this: