简体   繁体   English

获取画布之子

[英]Get Child of Canvas

I am manipulating Canvas delta, canvas has two children ( Rectangle and Thumb ). 我正在操纵Canvas delta,canvas有两个孩子( RectangleThumb )。 I want my manipulation delta to only work when I click on Rectangle . 我希望我的操作增量仅在单击Rectangle时起作用。 Is there a way I can get which Canvas child is clicked. 有没有一种方法可以让我单击哪个Canvas子级。

void rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    var recSender = (Canvas)sender; // Get the Rectangle
    var rec=recSender.Children[1] as Rectangle;

    //if (e.OriginalSource is Rectangle)
    {            
        TranslateTransform ttSender = recSender.RenderTransform as TranslateTransform; // Get the Rectangle's RenderTransform (which is a TranslateTransform)

        ttSender.X += e.Delta.Translation.X;
        ttSender.Y += e.Delta.Translation.Y;
    }
}

I'm not sure if I understood your problem fully. 我不确定我是否完全了解您的问题。 I think you have to set the Property 我认为您必须设置属性

 ManipulationMode="All"

of the child elements to be able to access them with e.OriginalSource. 可以使用e.OriginalSource访问它们的子元素。 This will make the event bubble up the chain. 这将使事件在整个链条中冒泡。

In my code it looks like this: 在我的代码中,它看起来像这样:

XAML XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Canvas ManipulationMode="All"  ManipulationDelta="Canvas_ManipulationDelta" >
            <Rectangle ManipulationMode="All" Fill="Red" Width="100" Height="411"></Rectangle>
            <Ellipse ManipulationMode="All" Fill="Green" Width="400" Height="130"></Ellipse>
        </Canvas>
    </Grid>

CS: CS:

 private void Canvas_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            var x = e.OriginalSource;    
        }

Now when I klick the ellipse I get: 现在,当我弹椭圆时,我得到: 原始源断点

So I got access to the object. 因此,我可以访问该对象。 Works on my machine running Windows 10, VS2015. 在运行Windows 10 VS2015的计算机上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM