2010-09-20 83 views
2

我是WPF中的新成员,我创建了一个新的UserControl MyUserControl。UserControl在WPF中的位置

现在我很惊讶:UserContol没有位置。

我该如何阅读(通过代码)myUserControl1。 Location在父容器中?

予解释:

我有一些点(用户控件),用户可以在一个面板拖动。其实,我不知道这将是什么样的面板...也许是网格。

现在,这些点应与一条线相连。

其实,我有一个Dot.HeadDot.Queue属性(也是点)。所以,当添加一个Head或Queue时,我需要在它们之间创建一个链接(Line)[A] ----- [B]。这为这条线我搜索开始和结束点设置。

控制XAML:

<UserControl x:Class="LinePlan.Stop" 
    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" 
    mc:Ignorable="d" d:DesignHeight="21" d:DesignWidth="80"> 
    <Canvas> 
     <Path Fill="LightBlue" Width="16" Height="16"> 
      <Path.Data> 
       <EllipseGeometry x:Name="Dot" Center="8,8" 
        RadiusX="4" RadiusY="4"/> 
      </Path.Data> 
     </Path> 
     <TextBlock x:Name="StopText" Text="Eiffel Tower" Canvas.Left="16"/> 
    </Canvas> 
</UserControl> 

代码:

public partial class Stop : UserControl 
{ 
    private Stop head; 
    private Stop tail; 
    private LineGeometry headLine; 
    private LineGeometry queueLine; 

    public Stop() 
    { 
     InitializeComponent(); 
    } 

    public Stop Head 
    { 
     get { return head; } 
     set 
     { 
      if (head != value) 
      { 
       head = value; 
       if (head == null) 
       { 
        if (headLine != null) 
         headLine = null; 
       } 
       else 
       { 
        headLine = new LineGeometry(); 
        headLine.StartPoint = head.DotPosition; 
        headLine.EndPoint = this.DotPosition; 

        // ?? Add this line to the parent 
       } 

      } 
     } 
    } 

    public Stop Tail 
    { 
     get { return tail; } 
     set { tail = value; } 
    } 

    public Point DotPosition 
    { 
     get 
     { 
      double x = Canvas.GetLeft(this) + this.Dot.Center.X; 
      double y = Canvas.GetTop(this) + this.Dot.Center.Y; 
      return new Point(x, y); 
     } 
     set 
     { 
      Canvas.SetLeft(this, value.X - this.Dot.Center.X); 
      Canvas.SetTop(this, value.Y - this.Dot.Center.Y); 
     } 
    } 
} 
+0

单词“队列”是一个人造*-AMI * ...我想你要找的字是“尾巴”;) – 2010-09-20 10:07:05

+0

@托马斯:好评 – serhio 2010-09-20 10:08:40

回答

3

的WPF布局系统不使用绝对定位,除非您将控件放置在支持绝对定位的容器上(通常为Canvas)。如果您使用的是Canvas,您可以获取或使用Canvas.LeftCanvas.RightCanvas.TopCanvas.Bottom附加属性设置控件的位置:

double x = Canvas.GetLeft(myControl); 
double y = Canvas.GetTop(myControl); 

现在,如果你想控制的实际位置(相对其父),你可以使用VisualTreeHelper.GetOffset方法:

Vector offset = VisualTreeHelper.GetOffset(myControl); 
double x = offset.X; 
double y = offset.Y; 
+0

谢谢。我在主题中加了一点解释。 – serhio 2010-09-20 09:52:24

+0

根据你的解释,我认为你唯一的候选面板是Canvas ... – 2010-09-20 10:09:05

+0

我添加了真实的代码... NAN返回为位置,至少在设计模式下... – serhio 2010-09-20 10:21:13

1

元素(如用户控件)通常放置在面板中WPF。根据您使用的面板,可能会向用户控件添加一些附加属性。如果将用户控件置于Canvas中,它将获得附加属性LeftTop,RightBottom。但是,如果用户控件放置在Grid中,它将获得附加属性和Column(以及更多)。其他面板如StackPanel不会附加任何属性。没有这样的东西作为通用用户控制位置。

Panels Overview

Attached Properties Overview

假设你使用的是Canvas作为面板,您可以访问附加LeftTop性质是这样的:

Double x = Canvas.GetLeft(myUserControl1); 
Double y = Canvas.GetTop(myUserControl1);