2010-09-20 82 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);