(WPF) 如何绑定到用户控件上的 IsMouseOver
编辑:问题的原始前提不正确,因此修改了问题:
基本上,我希望只有当鼠标位于包含的用户控件上时按钮才可见。 这是我所拥有的简化版本:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一旦托马斯指出我原来问题中的错误假设,我就修改了问题,这使我发现它在 这篇文章。
基本上,用户控件具有空背景(而不是透明),这显然使其对鼠标不可见,即使 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.
,但它有... IsMouseOver 是在 UIElement 类中定义的,UserControl (间接)继承自该类
But it does... IsMouseOver is defined in the UIElement class, from which UserControl (indirectly) inherits
您可以在派生类中实现该属性。 我以前也曾做过这样的事。
You could implement that property in a derived class. I've had to do this kind of thing before.