(WPF) 如何绑定到用户控件上的 IsMouseOver

发布于 2024-07-24 04:06:46 字数 596 浏览 9 评论 0原文

编辑:问题的原始前提不正确,因此修改了问题:

基本上,我希望只有当鼠标位于包含的用户控件上时按钮才可见。 这是我所拥有的简化版本:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNamespace.MyUserControl"
    x:Name="myUserControl">
    <Textbox>Some Text</Textbox>
    <Button Visibility="{Binding ElementName=myUserControl, Path=IsMouseOver, Converter={StaticResource mouseOverVisibilityConverter}}" />
</UserControl>

如果鼠标位于文本框上方,但不在用户控件中的其他任何位置,则该版本有效。

Edit: The original premise of the question was incorrect so revised the question:

Basically I want a button to be visible only when the mouse is over the containing user control. Here is the simplified versin of what I have:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNamespace.MyUserControl"
    x:Name="myUserControl">
    <Textbox>Some Text</Textbox>
    <Button Visibility="{Binding ElementName=myUserControl, Path=IsMouseOver, Converter={StaticResource mouseOverVisibilityConverter}}" />
</UserControl>

Which works if the mouse is over the text box, but not anywhere else in the user control.

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

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

发布评论

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

评论(3

晨曦慕雪 2024-07-31 04:06:46

一旦托马斯指出我原来问题中的错误假设,我就修改了问题,这使我发现它在 这篇文章

基本上,用户控件具有空背景(而不是透明),这显然使其对鼠标不可见,即使 IsHitTestVisible 设置为 true 也是如此,因此解决方案是将 Background="Transparent" 添加到用户控件。

I revised the question once Thomas pointed out the false assumption in my original question which lead me to discover the real reason it wasn't working in this post.

Basically the user control has a null background (as opposed to transparent) which apparently makes it invisible to the mouse, even with IsHitTestVisible set to true, so the solution was to add Background="Transparent" to the user control.

酷遇一生 2024-07-31 04:06:46

我意识到 UserControl 没有 IsMouseOver 属性

,但它有... IsMouseOver 是在 UIElement 类中定义的,UserControl (间接)继承自该类

I realized that UserControl doesn't have a IsMouseOver property

But it does... IsMouseOver is defined in the UIElement class, from which UserControl (indirectly) inherits

贩梦商人 2024-07-31 04:06:46

您可以在派生类中实现该属性。 我以前也曾做过这样的事。

Private _IsMouseOver As Boolean = False

Protected Overrides Sub OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = True
     MyBase.OnMouseEnter(sender, e)
End Sub

Protected Overrides Sub OnMouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = False
     MyBase.OnMouseLeave(sender, e)
End Sub

Public ReadOnly Property IsMouseOver As Boolean()
    Get
        Return _IsMouseOver
    End Get
End Property

You could implement that property in a derived class. I've had to do this kind of thing before.

Private _IsMouseOver As Boolean = False

Protected Overrides Sub OnMouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = True
     MyBase.OnMouseEnter(sender, e)
End Sub

Protected Overrides Sub OnMouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
     _IsMouseOver = False
     MyBase.OnMouseLeave(sender, e)
End Sub

Public ReadOnly Property IsMouseOver As Boolean()
    Get
        Return _IsMouseOver
    End Get
End Property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文