Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Canvas.GetLeft() always receive NaN value

Tags:

c#

wpf

I'm trying to getLeft of a control in WPF. But I always received the NaN value instead of double value. Here is what I'm trying:

XAML:

<Canvas Canvas.Left="230" Name="cnvSaver">
   <Border Focusable="False"  BorderBrush="#7FBFA379" BorderThickness="0 1" Width="460" Height="460">
       <Canvas Background="#3FFFFFFF" AllowDrop="True" Name="cnvViewerLower" Drop="cnvViewerLower_Drop" Height="460" Width="460">

       </Canvas>
   </Border>
   <Border Focusable="False" Name="borUpper" BorderBrush="#7FBFA379" BorderThickness="1 0">
       <Border.RenderTransform>
            <TranslateTransform x:Name="borUpperTranslate" X="0"/>
       </Border.RenderTransform>
       <Border.Triggers>
            <EventTrigger RoutedEvent="RadioButton.Checked">
                  <BeginStoryboard>
                       <Storyboard Name="stbUpperTranslate" Storyboard.TargetName="borUpperTranslate" Storyboard.TargetProperty="X">
                            <DoubleAnimation RepeatBehavior="Forever" Name="dbaUpperTranslate" From="230" To="0" Duration="0:0:3" AutoReverse="True"/>
                       </Storyboard>
                  </BeginStoryboard>
             </EventTrigger>
         </Border.Triggers>
         <Canvas Background="#3FEEDBB3" Drop="cnvViewerUpper_Drop" MouseLeftButtonDown="cnvViewerUpper_MouseLeftButtonDown" MouseMove="cnvViewerUpper_MouseMove" MouseLeftButtonUp="cnvViewerUpper_MouseLeftButtonUp" AllowDrop="True" Width="460" Height="460" Name="cnvViewerUpper"></Canvas>
      </Border>
</Canvas>

Code behind C#:

Point p = e.GetPosition(cnvSaver);
            if (x > p.X)
            {
                // Move to Left
                Border br = cnvViewerUpper.Parent as Border;
                double left = Canvas.GetLeft(br) - Math.Abs(p.X - x);
                if(left > 0){
                    Canvas.SetLeft(br, left);
                }
                MessageBox.Show("move to left");
            }
            else if (x == p.X)
            {
                MessageBox.Show("standing");
            }
            else {
                // Move to Right
                double left = Canvas.GetLeft(borUpper);
                if (left > 0)
                {
                    Canvas.SetLeft(borUpper, left);
                }
                MessageBox.Show("move to right");
            }

Thanks in advance.

like image 938
Ringo Avatar asked Dec 27 '13 03:12

Ringo


1 Answers

you can only get a value for Canvas.GetLeft for an element, when you have explicitly set it initially, otherwise it would return NaN(this is expected WPF behaviour).

same for Height and Width.

In your case, in your xaml, you haven't set any Canvas.Left property on your border, so when you access it in code, it will return NaN.

to get the actual left coordinates, use TranslatePoint

p = localItem.TranslatePoint(new Point(0, 0), br);
double currentLeft = p.X;
double currentTop = p.Y;
like image 52
gaurav5430 Avatar answered Nov 05 '22 06:11

gaurav5430