WPF 如何在两个相互重叠的 UIElement 上引发鼠标事件
WPF How to raise the mouse event on two UIElement overlaid to each other
我有两个 UIElement。 ViewBox 中的图像和覆盖它的 Canvas。两者都在后面的代码中注册了 MoseMove 事件。
当前仅在位于顶部的元素上引发事件。
有什么方法可以在两个 UIElement 上触发 MouseMove 吗?
XAML:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ClipToBounds="True">
<Viewbox Name="border" StretchDirection="Both" Stretch="Uniform" Panel.ZIndex="0" >
<Grid>
<Image Name="image" Panel.ZIndex="1" >
<Image.Source>
<BitmapImage UriSource="d:\test.png"/>
</Image.Source>
</Image>
</Grid>
</Viewbox>
<Canvas Name="canvas" Panel.ZIndex="2" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Window>
代码隐藏:
using System.Windows;
using System.Windows.Input;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
border.PreviewMouseMove += Border_PreviewMouseMove;
canvas.PreviewMouseMove += Canvas_PreviewMouseMove;
}
private void Border_PreviewMouseMove(object sender, MouseEventArgs e)
{
//Not Raised...
}
private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
//Raised...
}
}
}
你可以试试这个:)
private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
border.RaiseEvent(e);
}
我有两个 UIElement。 ViewBox 中的图像和覆盖它的 Canvas。两者都在后面的代码中注册了 MoseMove 事件。 当前仅在位于顶部的元素上引发事件。
有什么方法可以在两个 UIElement 上触发 MouseMove 吗?
XAML:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ClipToBounds="True">
<Viewbox Name="border" StretchDirection="Both" Stretch="Uniform" Panel.ZIndex="0" >
<Grid>
<Image Name="image" Panel.ZIndex="1" >
<Image.Source>
<BitmapImage UriSource="d:\test.png"/>
</Image.Source>
</Image>
</Grid>
</Viewbox>
<Canvas Name="canvas" Panel.ZIndex="2" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Window>
代码隐藏:
using System.Windows;
using System.Windows.Input;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
border.PreviewMouseMove += Border_PreviewMouseMove;
canvas.PreviewMouseMove += Canvas_PreviewMouseMove;
}
private void Border_PreviewMouseMove(object sender, MouseEventArgs e)
{
//Not Raised...
}
private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
//Raised...
}
}
}
你可以试试这个:)
private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
border.RaiseEvent(e);
}