在ViewBox / Canvas对象中查找子对象

时间:2010-10-21 18:57:41

标签: c# silverlight silverlight-4.0

我实现了一个主要在我的Silverlight 4应用程序中工作的拖放操作。一旦用户将形状放到画布上,我就想使用Size & Child Decorators。我试图实现示例代码。下面的代码是更大的应用程序的问题部分。

xmal -

<ScrollViewer Grid.RowSpan="1" Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="2" Name="scrollViewer">
    <Viewbox Margin="0"  MinWidth="400" MinHeight="500" 
        HorizontalAlignment="Left" VerticalAlignment="Top" Name="ViewBoxTestBuild">
        <Canvas x:Name="Camera1Canvas" telerikDragDrop:RadDragAndDropManager.AllowDrop="True" 
            Width="1200" Height="768" MouseLeftButtonDown="Camera1Canvas_MouseLeftButtonDown">
            <Image x:Name="Camera1Image" Source="timemagadj.jpg" Canvas.ZIndex="-1"  
                  HorizontalAlignment="Left" VerticalAlignment="Top" />
            <local:Three_Line_Graphic x:Name="threeLineEditTool" 
                  HorizontalAlignment="Left" Canvas.Left="594" Canvas.Top="621" />
            <l:Adorner x:Name="adorn" Canvas.ZIndex="100" />
        </Canvas>
    </Viewbox>
</ScrollViewer>

c#c​​ode -

private void Camera1Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var sendingObj = sender as Canvas;
            if (sendingObj == null) return;
            foreach (UserControl l in VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), sendingObj))
                {
                    if (l.Parent == Camera1Canvas )
                    {
                        adorn.AdornedElement = l as FrameworkElement;
                        adorn.adorned_MouseLeftButtonDown(l, e);
                        break;
                    }
                }
             base.OnMouseLeftButtonDown(e);
        }

我的问题是,当鼠标左键单击事件调用VisualTreeHelper.FindElementsInHostCoordinates时,单击任何对象时它不返回任何元素。我确定这是一个坐标映射问题,但由于这对我来说是个新的理由,我不确定如何修复它。

2 个答案:

答案 0 :(得分:2)

也许在你的情况下,不需要使用VisualTreeHelper方法。

由于您控制画布中的元素并且您知道它们的类型,您可以尝试这样的事情:

private void Camera1Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var sendingObj = sender as Canvas;
        if (sendingObj == null) return;
        foreach (UserControl l in sendingObj.Children)
            {
                var position = e.GetPosition(l);
                var lArea = new Rect(0,0,l.ActualWidth,l.ActualHeight);
                if (lArea.Contains(position))
                {
                    adorn.AdornedElement = l as FrameworkElement;
                    adorn.adorned_MouseLeftButtonDown(l, e);
                    break;
                }
            }
         base.OnMouseLeftButtonDown(e);
    }

哪个比命中测试更有效。但是,这仅适用于矩形区域可接受的情况。

如果需要检测矩形以外的形状,只要使用UserControl实例填充画布,就可以使用以下内容:

private void Camera1Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var sendingObj = sender as Canvas;
        if (sendingObj == null) return;

        var elements = VisualTreeHelper
             .FindElementsInHostCoordinates(
                 e.GetPosition(sendingObj), sendingObj);

        foreach (var l in elements)
        {
            if (l is UserControl)
            {
                adorn.AdornedElement = l as FrameworkElement;
                adorn.adorned_MouseLeftButtonDown(l, e);
                break;
            }
        }
        base.OnMouseLeftButtonDown(e);
    }

答案 1 :(得分:0)

您需要将VisualTreeHelper行更改为

GeneralTransform transform = sendingObj.TransformToVisual(Application.Current.RootVisual);
Point pnt = transform.Transform(e.GetPosition(sendingObj));
var elements = VisualTreeHelper.FindElementsInHostCoordinates(pnt,Application.Current.RootVisual);