簡體   English   中英

在Code Behind WPF中計算相對position

[英]Calculate relative position in Code Behind WPF

我如何在后面的代碼中計算我的元素(Lift、LArmPosition、PARmPosition)的相對 position,其中它們的 position 取決於 Canvas 寬度(X)和高度(Y)以及來自其他/的寬度(X)和高度(Y)相同的元素(Lift、LArmPosition、PARmPosition、Rectanngle)。

我現在有絕對值,例如 max (570) 和 min (80),它們是硬編碼的,用於向左、向右、向上和向下移動我的元素。

這是我的 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>

您使用Canvas.LeftCanvas.Top附加屬性設置相對於父Canvas左上角的元素的 position。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM