我正在wpf工作。我有一个触发鼠标悬停的问题。
我有两个按钮Button A和ButtonB。当我在Button B上输入鼠标时,我需要触发Button B的MouseOver。
解决方案是C#或XAML。
我对这个问题一无所知。该怎么做?
答案 0 :(得分:0)
所有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);
}