0

我正在 WPF 中设计一个 UserControl。有一个带有一些路径的画布。在这些路径中,我有两个椭圆(图片的顶部和底部)。当 Ellipses 事件被触发时,我正在做一些工作,MouseUp但是当用户单击加/减路径时,它们没有触发!我见过这个这个。但似乎这里的冒泡和隧道效应并非如此,因为椭圆中不包含减/加路径。这是其中一个椭圆的代码:

<Canvas Width="96" Height="550" MouseUp="plusPath_MouseUp" Background="Transparent">
    <Path x:Name="Path" Width="96" Height="550" Canvas.Left="0"  StrokeThickness="6" StrokeLineJoin="Round" Stroke="#FF465546" Canvas.Top="0" Stretch="Fill" Data="..."/>
    <Ellipse x:Name="zoomIn"  Width="68" Height="68" Canvas.Left="14" Canvas.Top="18.5143" />
    <Ellipse x:Name="zoomOut" Width="68" Height="68" Canvas.Left="14" Canvas.Top="468.79" />
    <Path  x:Name="minusPath" Cursor="Hand" Width="36" Height="6" Canvas.Left="30" Canvas.Top="500" Stretch="Fill" StrokeThickness="6" StrokeLineJoin="Round" Stroke="#FF87A698" Data="F1 M 33.0001,501.956L 63.0001,501.956"/>
    <Path  x:Name="plusPath" Cursor="Hand" Width="36.0001" Height="36" Canvas.Left="30" Canvas.Top="34" Stretch="Fill" StrokeThickness="6" StrokeLineJoin="Round" Stroke="#FF87A698" Data="M 34.0658,52.181L 64.0659,52.181M 49.0657,67.181L 49.0657,37.181"/>
</Canvas>

我应该处理MouseUp减/加路径的事件还是有更好的方法?

编辑:我正在寻找最佳实践。

在此处输入图像描述

4

2 回答 2

2

处理MouseUp容器元素(比如 Grid、DockPanel、Canvas 等),因为每当在子元素(Ellipse 或 Path)上引发 MouseUp 事件时,它都会冒泡到根元素(Canvas)

<Canvas MouseUp="Handler">
   <Ellipse/>
   <Path/>
</Canvas>

e.OriginalSource您可以通过检查是否想知道哪个元素实际引发事件来获取事件的原始发送者。(椭圆或路径)。

private void Handler(object sender, MouseButtonEventArgs e)
{
    // Check actual sender here which can be Canvas, Ellipse or Path.
    Ellipse senderObject = e.OriginalSource as Ellipse;
    if(senderObject == null)
    {
       Path senderPath = e.OriginalSource as Path;
    }
}
于 2014-01-29T11:38:16.650 回答
1

您可以将两者都Ellipse放入PathGrid捕获MouseUp="zoomIn_MouseUp"

<Grid Cursor="Hand" MouseUp="zoomIn_MouseUp" Canvas.Left="14" Canvas.Top="18.5143">
    <Ellipse x:Name="zoomIn"  Width="68" Height="68"/>
    <Path x:Name="plusPath" StrokeThickness="6" Width="36.0001" Height="36" StrokeLineJoin="Round" Stretch="Fill" Stroke="#FF87A698" Data="M 34.0658,52.181L 64.0659,52.181M 49.0657,67.181L 49.0657,37.181"/>
</Grid>
于 2014-01-29T11:45:51.533 回答