Igor Adamenko
Igor Adamenko

Reputation: 143

Detect grid upon grid in XAML

I have 2 Grids in XAML. One small and one big. How I can detect that, after first move, it upon second? I need smth like coordinates (need to align the first grid to the second grid's border or to other grids in this 'big-grid'). What're methods, properties?

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="Parent" Margin="124,340,1042,228">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" ManipulationDelta="Object_ManipulationDelta" x:Name="Figure" ManipulationMode="All">
            <Rectangle Fill="Red" Width="40" Height="40"/>
            <Rectangle Fill="Red" Width="40" Height="40" Margin="-10,30,70,30"/>
            <Rectangle Fill="Red" Width="40" Height="40" Margin="-10,70,70,-10"/>
            <Rectangle Fill="Red" Width="40" Height="40" Margin="-50,70,110,-10"/>
            <Grid.RenderTransform>
                <CompositeTransform/>
            </Grid.RenderTransform>
        </Grid>
    </Grid>
    <Grid x:Name="Field" Width="500" Height="700">
        <Rectangle Fill="Black" Width="40" Height="40" Margin="10,10,450,650" StrokeThickness="1" Stroke="#FF1B1B1B"/>
        <Rectangle Fill="Black" Width="40" Height="40" Margin="50,10,410,650" StrokeThickness="1" Stroke="#FF1B1B1B"/>
        <Rectangle Fill="Black" Width="40" Height="40" Margin="90,10,370,650" StrokeThickness="1" Stroke="#FF1B1B1B"/>
        <!--many rectangles-->
    </Grid>
</Grid>

I move Grid Figure to Grid Field. In future I may be change rectangle to border.

Upvotes: 0

Views: 88

Answers (1)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

To detect the relative position between Figure and Field, you can use something like that:

var transform= Field.TransformToVisual(Figure);
Point relativePosition = transform.TransformPoint(new Point(0,0));

relativeCoordinate will be the distance between the Top Left of Field and the Top left of Figure

if you want the absolute coordinate of the Grid you can do this:

 var transform= this.TransformToVisual(Figure);
 Point absolutePositionFigureGrid = transform.TransformPoint(new Point(0,0));

Upvotes: 1

Related Questions