【问题标题】:How to trigger one button's MouseOver depending on another button's MouseEnter?如何根据另一个按钮的 MouseEnter 触发一个按钮 MouseOver?
【发布时间】:2019-05-16 10:12:23
【问题描述】:

我在 wpf 工作。我在触发鼠标悬停时遇到问题。

我有两个按钮 Button A 和 Button B。当我在 Button B 上输入鼠标时,我需要触发 Button B 的 MouseOver。

解决方案是 C# 或 XAML。

我对这个问题没有任何想法。如何做到这一点?

【问题讨论】:

标签: wpf xaml wpf-controls c#-3.0 wpf-4.0


【解决方案1】:

所有 UIElement 都有 RaiseEvent() 方法。在该方法中,您可以传递 MouseEventArgs 的对象。

XAML:

            <StackPanel Orientation="Vertical">
                <Button Name="buttonA" Click="buttonA_Click" Content="ButtonA" Height="25" Width="80" 
                MouseEnter="buttonA_MouseEnter" Margin="10 5 2 2" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <Button Name="buttonB" Click="buttonB_Click" MouseEnter="buttonB_MouseEnter" Content="ButtonB" Height="25" Width="80" Margin="10 5 2 2"
                VerticalAlignment="Center" HorizontalAlignment="Left"/>
            </StackPanel>

xaml.cs:

            private void buttonA_MouseEnter(object sender, MouseEventArgs e)
            {
                MessageBox.Show("Message from Button A...! ");
            }

            private void buttonB_MouseEnter(object sender, MouseEventArgs e)
            {
                MouseEventArgs mouseEventArgs = new MouseEventArgs(Mouse.PrimaryDevice, 0);
                mouseEventArgs.RoutedEvent = Mouse.MouseEnterEvent;
                buttonA.RaiseEvent(mouseEventArgs);
            }

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2011-11-23
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多