Home > Blockchain >  Calculate relative position in Code Behind WPF
Calculate relative position in Code Behind WPF

Time:11-29

How can I calculate relative position of my elements (Lift, LArmPosition, PArmPosition) in code behind, where their position depends on the Canvas Width(X) and Height(Y) and the Width(X) and Height(Y) from other/same elements(Lift, LArmPosition, PArmPosition, Rectanngle).

I have absolute values now, for example max (570) and min (80), which are hard coded, for moving my elements left, right, up and down.

Here is my XAML:

<UserControl x:Class="WPF_AnimatedLift.View.Prozess"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPF_AnimatedLift.View" xmlns:viewmodel="clr-namespace:WPF_AnimatedLift.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="1000" Background="WhiteSmoke">
    <Canvas x:Name="MyCanvas" Margin="0,0,-163,-83">

        <Path x:Name="Lift" Fill="#FF808080" Data="m 67.621819 110.44866 v 16.55811 H 176.13074 v -16.55811 z m 0.143117 16.70929 v 1.59777 h 0.743496 v 4.86682 h 18.534155 v -4.86682 H 140.4094 v 4.84489 h 18.53416 v -4.84489 h 17.04716 v -1.59777 z m 93.167904 1.59774 c 0.10872 56.40049 0.026 57.01222 0.026 57.01222 l 15.18908 17.62703 -0.005 -74.7721 -15.21024 0.13288" Canvas.Left="600.622" Canvas.Top="{Binding LiftScreenPosition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="175.277" Stretch="Fill" Width="185.378"/>

        <Rectangle Canvas.Left="786" Width="150" Height="449" Fill="#FFB3B3B3" StrokeThickness="0.264583" Canvas.Top="114" x:Name="Rectanngle"/>

        <Path x:Name="LArmPosition" Fill="#FFB3B3B3" StrokeThickness="0.251214" Stroke="#FF000000" StrokeMiterLimit="4" Data="M 83.254456 115.22409 H 118.4004 v 17.92124 H 83.254456 Z m -10.289864 17.79897 h 55.592058 v 3.17904 H 72.964592 Z m -3.608147 3.17904 h 62.140165 v 39.12671 H 69.356445 Z" Height="70" Canvas.Left="{Binding LArmPositionX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Stretch="Fill" Canvas.Top="{Binding LArmPositionY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="78"/>

        <Rectangle Canvas.Left="{Binding PArmPositionX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Canvas.Top="{Binding LiftScreenPosition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="214" Height="15" x:Name="PArmPosition" Fill="#FFCCCCCC" StrokeThickness="0.264583"/>

        <Path x:Name="ClampR" Fill="#FF000000" StrokeThickness="2.12787" Data="M 524.2207 473.57227 L 524.2207 563.57227 L 137.14258 563.57227 L 137.14258 652.14258 L 571.42773 652.14258 L 571.42773 563.57227 L 571.36328 563.57227 L 571.36328 473.57227 L 524.2207 473.57227 z " Height="63.65" Stretch="Fill" Width="115.168" Canvas.Left="{Binding RightClampPositionX}" Canvas.Top="{Binding ClampsPositionY}" >
            <Path.LayoutTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="0.265" ScaleX="0.265"/>
                    <SkewTransform/>
                    <RotateTransform CenterX="0.5" CenterY="0.5" Angle="{Binding ClampRechtsAngle}"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Path.LayoutTransform>
        </Path>

        <Path x:Name="ClampL" Fill="#FF000000" StrokeThickness="2.12787" Data="M 110 344.28516 L 110 434.28516 L 110 485.71484 L 110 522.85742 L 544.28516 522.85742 L 544.28516 434.28516 L 157.14258 434.28516 L 157.14258 344.28516 L 110 344.28516 z " Height="63.908" Stretch="Fill" Width="113.752" Canvas.Left="{Binding LeftClampPositionX}" Canvas.Top="{Binding ClampsPositionY}">
            <Path.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="0.265" ScaleX="0.265"/>
                    <SkewTransform/>
                    <RotateTransform Angle="{Binding ClampLinksAngle}"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Path.RenderTransform>
        </Path>

        <TextBox Height="30" Canvas.Left="176" TextWrapping="Wrap" Canvas.Top="203" Width="120" FontSize="20" Text="{Binding PArmPositionInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

        <Label Content="PArmPosition" Canvas.Left="65" Canvas.Top="204" FontWeight="Bold" FontSize="15"/>

        <TextBox x:Name="TxtBoxY" Height="30" Canvas.Left="156" TextWrapping="Wrap" Canvas.Top="84" Width="120" FontSize="20" Text="{Binding LiftPosInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" UndoLimit="600"/>

        <Label Content="LiftPosition" Canvas.Left="48" Canvas.Top="84" FontWeight="Bold" FontSize="15" Width="104"/>

        <Button Content="START" Canvas.Left="135" Canvas.Top="403" Width="106" Height="48" FontSize="30" FontWeight="Bold" Command="{Binding StartCommand}"/>
        <Button Content="{Binding ButtonMessage}" Canvas.Left="135" Canvas.Top="486" Width="200" FontSize="20" Height="37" Command="{Binding ChangeClampStateCommand}"/>
        <TextBox Height="30" Canvas.Left="179" TextWrapping="Wrap" Canvas.Top="313" Width="120" FontSize="20" Text="{Binding LArmPositionInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <Label Content="LArmPosition" Canvas.Left="69" Canvas.Top="314" FontWeight="Bold" FontSize="15"/>

    </Canvas>
    <UserControl.DataContext>
        <viewmodel:ProzessViewModel/>
    </UserControl.DataContext>
</UserControl>

CodePudding user response:

You set the position of an element relative to the top-left corner of the parent Canvas using the Canvas.Left and Canvas.Top attached properties.

  • Related