我正在WPF中设计一个UserControl。有一个带有一些路径的画布。在这些路径中,我有两个椭圆(图片的顶部和底部)。我在Ellipses的MouseUp
事件被触发时做了一些工作,但是当用户点击加/减路径时它们没有被触发!我看过this和this。但似乎在这里冒泡和隧道不是这种情况,因为减去/加号路径不包含在椭圆中。以下是其中一个省略号的代码:
<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
事件还是有更好的方法?
编辑:我正在寻找最佳做法。
答案 0 :(得分:2)
处理容器元素上的MouseUp
(比如Grid,DockPanel,Canvas等),因为只要在子节点(Ellipse或Path)上引发MouseUp事件, 就会冒泡到根节点元素(画布) 。
<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;
}
}
答案 1 :(得分:1)
您可以将Ellipse
和Path
放入Grid
并抓住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>