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

Dav*_*vy8 2 data-binding wpf user-controls mouseover

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

基本上我只想在鼠标悬停在包含的用户控件上时才能看到一个按钮.这是我所拥有的简化版本:

<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>
Run Code Online (Sandbox Code Playgroud)

如果鼠标在文本框上方,但在用户控件中没有其他任何位置,则该方法有效.

Dav*_*vy8 6

一旦托马斯在我原来的问题中指出了错误的假设,这导致我发现它在这篇文章中不起作用的真正原因,我就修改了这个问题.

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