简体   繁体   English

为什么鼠标事件不能与父对象中的相同事件一起使用?

[英]Why do mouse events not work with same events in parent object?

I have two borders that are replacing each other via animation, triggered by MouseEnter and MouseLeave events.我有两个通过动画相互替换的边框,由MouseEnterMouseLeave事件触发。 And I also have a button on one of the borders with MouseEnter="Test_MouseEnter" MouseDown="Test_MouseDown" .而且我还有一个带有MouseEnter="Test_MouseEnter" MouseDown="Test_MouseDown"的边框上的按钮。
XAML: XAML:

<UserControl.Resources>
        <Storyboard x:Key="FirstCardStoryboard">
            <DoubleAnimation
                 Duration="0:0:0.2"
                Storyboard.TargetName="FirstBorder" 
                Storyboard.TargetProperty="Opacity"/>
        </Storyboard>
        <Storyboard x:Key="SecondCardStoryboard">
            <DoubleAnimation
                 Duration="0:0:0.2"
                Storyboard.TargetName="SecondBorder" 
                Storyboard.TargetProperty="Opacity"/>
        </Storyboard>
    </UserControl.Resources>
    
    <Grid MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave">
        <Border x:Name="FirstBorder" Width="170" Height="210" CornerRadius="15" BorderThickness="2" Background="#FF323236" BorderBrush="Cyan">
            <StackPanel VerticalAlignment="Center">
                <Button x:Name="Test" Width="130" Height="30" MouseEnter="Test_MouseEnter" Click="Test_Click">
                </Button>
            </StackPanel>
        </Border>
        <Border x:Name="SecondBorder" Width="170" Height="210" CornerRadius="15" BorderThickness="2" 
                Background="#FF323236" BorderBrush="Cyan" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="40"/>
                </Grid.RowDefinitions>

            </Grid>
        </Border>
    </Grid>

And the C# side:而 C# 方面:

public partial class ProductControl : UserControl
{
    public ProductControl()
    {
        InitializeComponent();
    }

    private void Grid_MouseEnter(object sender, MouseEventArgs e)
    {
        Storyboard storyboard = (this.Resources["FirstCardStoryboard"] as Storyboard);
        DoubleAnimation animation = storyboard.Children.First() as DoubleAnimation;
        animation.To = 1;
        storyboard.Begin();

        Storyboard storyboardSecond = (this.Resources["SecondCardStoryboard"] as Storyboard);
        DoubleAnimation animationSecond = storyboardSecond.Children.First() as DoubleAnimation;
        animationSecond.To = 0;
        storyboardSecond.Begin();
    }

    private void Grid_MouseLeave(object sender, MouseEventArgs e)
    {
        Storyboard storyboard = (this.Resources["FirstCardStoryboard"] as Storyboard);
        DoubleAnimation animation = storyboard.Children.First() as DoubleAnimation;
        animation.To = 0;
        storyboard.Begin();

        Storyboard storyboardSecond = (this.Resources["SecondCardStoryboard"] as Storyboard);
        DoubleAnimation animationSecond = storyboardSecond.Children.First() as DoubleAnimation;
        animationSecond.To = 1;
        storyboardSecond.Begin();
    }

    private void Test_MouseEnter(object sender, MouseEventArgs e)
    {

    }

    private void Test_Click(object sender, RoutedEventArgs e)
    {
        
    }

}

Then I put breakpoints on Test_MouseEnter and Test_MouseDown methods, but it seems that they do not call, when I move mouse over the button or click on it.然后我在Test_MouseEnterTest_MouseDown方法上设置了断点,但是当我将鼠标移到按钮上或单击它时,它们似乎没有调用。
Is this because of the Grid with MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave" or is there another reason of this?这是因为带有MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave"网格还是还有其他原因? If the Grid stops events, is the a way to pass on this events?如果网格停止事件,是传递这些事件的方法吗?

I tested Click="Test_Click" on the button, but this also doesn't work.我在按钮上测试了Click="Test_Click" ,但这也不起作用。

I found the problem.我发现了问题。 I was changing only opacity of the borders, but they were still there.我只改变了边界的不透明度,但它们仍然存在。 So the Second Border handled all the events.所以第二边境处理了所有的事件。 So I also need to change Visibility of the borders in animation.所以我还需要改变动画中边框的可见性
Or as @Clemens said I can change just IsHitTestVisible .或者正如@Clemens 所说,我可以只更改 IsHitTestVisible

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM