我是WPF的新手,我创建了一个新的UserControl MyUserControl。
现在我很惊讶:UserContol没有位置。
如何在父容器中读取(通过代码)myUserControl1。Location
?
我解释说:
我有一些用户可以在面板中拖动的Dots(UserControls)。实际上,我不确定这将是什么样的Panel ......也许是Grid。
现在,这些点应该与Line相关联。
实际上,我有Dot.Head
和Dot.Queue
属性(也是Dots)。因此,当添加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 :(得分:3)
WPF布局系统不使用绝对定位,除非您将控件放在支持绝对定位的容器上(通常为Canvas
)。如果您使用的是Canvas
,则可以使用Canvas.Left
,Canvas.Right
,Canvas.Top
和Canvas.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;
答案 1 :(得分:1)
元素(如用户控件)通常放在WPF中的面板中。根据您使用的面板,面板可能会向用户控件添加一些附加属性。如果用户控件放在Canvas
中,则会获得附加的属性Left
,Top
,Right
和Bottom
。但是,如果用户控件放在Grid
中,则会获得附加属性Row
和Column
(以及更多内容)。像StackPanel
这样的其他面板不会附加任何属性。没有通用的用户控制位置。
假设您使用Canvas
作为面板,可以访问附加的Left
和Top
属性,如下所示:
Double x = Canvas.GetLeft(myUserControl1);
Double y = Canvas.GetTop(myUserControl1);