我怎样才能获得startX和startY位置?

时间:2010-12-30 15:44:48

标签: silverlight

如何获得rectToGetXAndY的startX和startY位置。这个功能对我的应用程序非常关键,但它让我发疯。我想到的唯一方法是让用户手动点击网格的左上边框,然后处理mouseleftbuttondown事件。显然这不是我想要的解决方案。这是我的代码: -

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="DelSilverlightApp.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="DarkSlateGray">
        <Grid x:Name="rectToGetXAndY" Background="HotPink" Width="300" Height="300" HorizontalAlignment="Center" VerticalAlignment="Center">

        </Grid>
    </Grid>
</UserControl>

编辑: -

这背后的代码: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DelSilverlightApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            GeneralTransform gt = rectToGetXAndY.TransformToVisual(null);
            Point p = gt.Transform(new Point(0, 0));
            MessageBox.Show(p.X + " " + p.Y);
        }
    }
}

提前致谢:)

3 个答案:

答案 0 :(得分:3)

我使用以下代码使用@AnthonyWJones的代码:

XAML

        <UserControl x:Class="GetPositionUi.MainPage"
            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"
            mc:Ignorable="d"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            d:DesignHeight="300" d:DesignWidth="400">

            <Grid x:Name="LayoutRoot" Background="DarkSlateGray">
                <Grid x:Name="rectToGetXAndY" 
                        Background="HotPink" 
                        Width="300" 
                        Height="300" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center">
                    <TextBlock x:Name="PositionTextBlock" Text="{Binding Path=ReferencePosition}"/>
                </Grid>
            </Grid>
        </UserControl>

代码背后:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace GetPositionUi
    {
        public partial class MainPage : UserControl
        {

            #region ReferencePosition

            /// <summary>
            /// ReferencePosition Dependency Property
            /// </summary>
            public static readonly DependencyProperty ReferencePositionProperty =
                DependencyProperty.Register("ReferencePosition", typeof(Point), typeof(MainPage),
                    new PropertyMetadata((Point)(new Point(0, 0)),
                        new PropertyChangedCallback(OnReferencePositionChanged)));

            /// <summary>
            /// Gets or sets the ReferencePosition property.  This dependency property 
            /// indicates the reference position of the child element.
            /// </summary>
            public Point ReferencePosition
            {
                get { return (Point)GetValue(ReferencePositionProperty); }
                set { SetValue(ReferencePositionProperty, value); }
            }

            /// <summary>
            /// Handles changes to the ReferencePosition property.
            /// </summary>
            private static void OnReferencePositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                ((MainPage)d).OnReferencePositionChanged(e);
            }

            /// <summary>
            /// Provides derived classes an opportunity to handle changes to the ReferencePosition property.
            /// </summary>
            protected virtual void OnReferencePositionChanged(DependencyPropertyChangedEventArgs e)
            {
            }

            #endregion

            public MainPage()
            {
                InitializeComponent();
            }

            protected override Size ArrangeOverride(Size finalSize)
            {
                var arrangedSize = base.ArrangeOverride(finalSize);
                GeneralTransform gt = rectToGetXAndY.TransformToVisual(LayoutRoot);
                Point p = gt.Transform(new Point(0, 0));
                ReferencePosition = p;
                return arrangedSize;
            }
        }
    }

这里的关键是让基座先排列控件,然后使用变换找到位置,最后返回新的arrangeSize。

我不建议此时显示一个消息框,但您可以使用依赖属性更改的回调来使用更新的位置执行任何操作。

答案 1 :(得分:2)

在Silveright中,您可以使用此代码确定rectToGetXAndY相对于LayoutRoot的当前X和Y位置: -

GeneralTransform gt = rectToGetXAndY.TransformToVisual(LayoutRoot);
Point p = gt.Transform(new Point(0, 0));

答案 2 :(得分:0)

您可以使用VisualTreeHelper ...

Vector vector = VisualTreeHelper.GetOffset(rectToGetXAndY);
Point currentPoint = new Point(vector.X, vector.Y);