为什么鼠标事件在父对象中不适用于相同事件?

发布于 2025-02-03 09:01:13 字数 3535 浏览 3 评论 0原文

我有两个边界通过动画互相替换,由 mouseenter mouseleave 事件触发。而且我在一个边界上还有一个按钮,mouseenter =“ test_mouseenter” mousedown =“ test_mousedown”
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>

和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)
    {
        
    }

}

然后,我将断点放在 test_mouseenter test_mousedown 方法上它。
这是因为 grid 带有mouseenter =“ grid_mouseenter” mouseleave =“ grid_mouseleave”还是还有另一个原因?如果网格停止事件,这是通过此事件的方法吗?

我在按钮上测试了 click =“ test_click” ,但这也不起作用。

I have two borders that are replacing each other via animation, triggered by MouseEnter and MouseLeave events. And I also have a button on one of the borders with MouseEnter="Test_MouseEnter" MouseDown="Test_MouseDown".
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:

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.
Is this because of the Grid with MouseEnter="Grid_MouseEnter" MouseLeave="Grid_MouseLeave" or is there another reason of this? 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.

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

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

发布评论

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

评论(1

人事已非 2025-02-10 09:01:13

我发现了这个问题。我只改变了边界的不透明度,但它们仍然在那里。因此,第二个边境处理了所有事件。因此,我还需要更改动画中边界的可见性。
或正如@clemens所说,我只能更改 iShittestvisible

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.

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