WPF Canvas.GetLeft() 总是接收 NaN 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20794279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:18:27  来源:igfitidea点击:

WPF Canvas.GetLeft() always receive NaN value

c#wpf

提问by Ringo

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:

我正在尝试在 WPF 中获取控件的左侧。但我总是收到 NaN 值而不是 double 值。这是我正在尝试的:

XAML:

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

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.

提前致谢。

回答by gaurav5430

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

您只能Canvas.GetLeft在最初明确设置元素时获取元素的值,否则它将返回NaN(这是预期的WPF行为)。

same for Height and Width.

对于Height and Width.

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

就您而言,在您的 中xaml,您尚未在 上设置任何Canvas.Left属性border,因此当您在代码中访问它时,它将返回NaN.

to get the actual leftcoordinates, use TranslatePoint

要获得实际left坐标,请使用TranslatePoint

p = localItem.TranslatePoint(new Point(0, 0), br);
double currentLeft = p.X;
double currentTop = p.Y;