WPF 中的鼠标悬停事件?
我正在尝试在 WPF MVVM 应用程序中实现工具提示系统。工具提示应该出现在作为主窗口一部分的某个标签中(不是悬停在鼠标指针处),并且应该在鼠标悬停时发生(理想情况下具有可配置的延迟,但这并不重要),调用方法在适当的 ViewModel 中。然后,ViewModel 将处理与模型层的联系,该模型层将解决要显示哪个工具提示等问题。
我的问题是如何从视图到 ViewModel 获取鼠标悬停事件,以及触发该事件的对象的标识(这样我就可以解决它的正确工具提示)。假设我的 SettingsView.xaml 中有一个 ,如何让它调用
GetTooltip( string id)
方法在 SettingsViewModel 中使用 MyLabel 作为鼠标悬停时的参数(或以其他方式可访问)?我可以使用常规的
来执行此操作吗?
I'm trying to implement a tool-tip system in a WPF MVVM application. The tool-tip is supposed to appear in a certain Label that is a part of the MainWindow (not hovering at the mouse pointer) and should happen on mouse-over (ideally with a configurable delay, but that is unimportant), calling a method in the appropriate ViewModel. The ViewModel will then deal with contacting the Model layer that will resolve which tool-tip to display etc.
My problem is how to get a mouse-over event from the view to the ViewModel, along with the identity of the object that fired the event (so I can resolve the right tool-tip for it). Let's say I have a <Label Name="MyLabel" Content="This is a label" />
in my SettingsView.xaml, how do I get it to call the GetTooltip(string id)
method in SettingsViewModel with MyLabel as the argument (or otherwise accessible) on mouse-over? Can I use a regular <Trigger>
to do it somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要做的是将一些命令(ViewModel 与视图中的事件交互的首选方法)连接到鼠标事件,然后使用适当的信息“填充”您的“工具提示”控件。
例如,您的 ViewModel 有一个 ToolTip 属性(表示单向,仅获取 ToolTipViewModel),该属性可以绑定到 UI 的某个区域(使用 DataTemplate)。您绑定到新的“MouseOver”命令,并根据命令填充/取消 ToolTipViewModel 属性中的对象。这允许可测试性、零代码隐藏以及使用 DataTemplates 构建工具提示演示文稿。
查看此链接 看看在通常的“非命令”控件上构建命令并绑定到它们是否可以解决您的问题。
What you need to do is hook up some commands (the preferred approach for ViewModels to interact with events in the View) to the Mouse events and then "populate" your "tooltip" control with the appropriate information.
For example, your ViewModel has a ToolTip property (that represents a one-way, get only ToolTipViewModel) that can be bound to an area of your UI (with a DataTemplate). You bind up to the new "MouseOver" command and populate/nullify the object that is in your ToolTipViewModel property based on the commands. This allows for testability, zero-code behind, and using DataTemplates to build your tooltip presentation.
Check out this link to see if building Commands and binding to them on normally "Uncommanded" controls might solve your problem.
首先,您是否尝试过内置的
ToolTip
控件?它可能会免费为您提供所需的内容,并且可以对其进行样式/模板化以托管复杂的内容。其次,您可以使用
每个 WPF 控件上都提供 MouseEnter
事件。不过,您必须将处理程序附加到您想要支持的控件。为了一般性地覆盖每个控件,请考虑将处理程序附加到父
Window
的PreviewMouseMove
事件(或覆盖窗口代码隐藏中的OnPreviewMouseMove
),每当任何子元素都将收到一个。然后,您可以使用HitTest
找出鼠标指针下方的控件。不过,这并不是非常有效,这就是为什么使用内置ToolTip
控件可能会更好。First, have you tried the built-in
ToolTip
control? It might give you what you need for free, and it can be styled/templated to host complex content.Second, you may be able to use the
MouseEnter
event available on every WPF control. You'd have to attach your handler to the controls you want to support, though.In order to generically cover every control, consider attaching a handler to the parent
Window
'sPreviewMouseMove
event (or overrideOnPreviewMouseMove
in the Window's code-behind), which will be called whenever any child element is about to receive one. You can then find out what control is under the mouse pointer by usingHitTest
. This isn't terribly efficient, though, which is why you may be better-off with the built-inToolTip
control.在控件模板中创建一个带有嵌套 TextBlock 的 Popup 控件(为标签创建样式),将其可见性属性绑定到 IsMouseOver 时,并将 PlacementTarget 绑定到希望其出现的窗口底部的控件。使用水平/垂直偏移属性进行微调。
Create a Popup control with a nested TextBlock in your control template (make a style for the label), bind its visibility property to when IsMouseOver, and bind the PlacementTarget to the control at the bottom of the window where you want it to appear. Finetune with Horizontal/Vertical offset properties.