How can I get startX and startY?

How can I get startX and startY positions rectToGetXAndY. This functionality is very important for my application, but it drives me crazy. The only approach that comes to my mind is to ask the user to manually click on the upper left border of the grid, and then handle the mouseleftbuttondown event. Obviously this is not the solution I want. Here is my code: -

<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>

EDIT: -

This code is behind: -

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);
        }
    }
}

Thanks in advance:)

+3
source share
3 answers

I did this using @AnthonyWJones code using the following:

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>

Code behind:

    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;
            }
        }
    }

The key here allows the base device to arrange the controls first, then use the transform to find the position, and finally return the new line format.

, , , , .

+3

Silveright X Y rectToGetXAndY LayoutRoot: -

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

You can use VisualTreeHelper ...

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

Source: https://habr.com/ru/post/1782796/


All Articles