用户控件的位置在WPF [英] UserControl Location in WPF

查看:134
本文介绍了用户控件的位置在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新WPF中,我创建了一个新的用户控件的MyUserControl。

现在我觉得很奇怪:在UserContol没有一个位置

我怎么能读(由code)myUserControl1。位置在父容器?

我解释一下:

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

现在,这些点应该用线相连。

其实,我有一个 Dot.Head Dot.Queue 属性(也点)。所以,当一个头部或队列加,我需要dinamically创建它们之间的联系(线)[A] ----- [B]。这对于这条线我搜索的起点和终点设置。

控制XAML:

 <用户控件X:类=LinePlan.Stop
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
    的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
    MC:可忽略=DD:DesignHeight =21D:DesignWidth =80>
    <画布>
        <路径填充=浅蓝色WIDTH =16高度=16>
            < Path.Data>
                < EllipseGeometry X:名称=点中心=8,8
                    半径X =4半径=4/>
            < /Path.Data>
        < /路径>
        < TextBlock的X:名称=StopText文本=艾菲尔铁塔Canvas.Left =16/>
    < /帆布>
< /用户控件>
 

code:

 公共部分类停止:用户控件
{
    私人停止头;
    私人停止尾;
    私人LineGeometry标题;
    私人LineGeometry queueLine;

    公共停止()
    {
        的InitializeComponent();
    }

    市民停止头
    {
        {返回头; }
        组
        {
            如果(头!=值)
            {
                头=价值;
                如果(头== NULL)
                {
                    如果(标题!= NULL)
                        标题= NULL;
                }
                其他
                {
                    标题=新LineGeometry();
                    headLine.StartPoint = head.DotPosition;
                    headLine.EndPoint = this.DotPosition;

                    //?添加此行到父
                }

            }
        }
    }

    市民停止尾
    {
        {返回尾; }
        集合{尾=价值; }
    }

    公共点DotPosition
    {
        得到
        {
            双X = Canvas.GetLeft(本)+ this.Dot.Center.X;
            双Y = Canvas.GetTop(本)+ this.Dot.Center.Y;
            返回新的点(X,Y);
        }
        组
        {
            Canvas.SetLeft(这一点,value.X  -  this.Dot.Center.X);
            Canvas.SetTop(这一点,value.Y  -  this.Dot.Center.Y);
        }
    }
}
 

解决方案

在WPF布局系统不使用绝对定位,除非你把你的控件在支持绝对定位的容器(通常为帆布)。如果您使用的是画布,您可以获取或使用设置控件的位置 Canvas.Left Canvas.Right Canvas.Top Canvas.Bottom 附加属性:

 双X = Canvas.GetLeft(myControl);
双Y = Canvas.GetTop(myControl);
 

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

 矢量偏移= VisualTreeHelper.GetOffset(myControl);
双X = offset.X;
双Y = offset.Y;
 

I am new in WPF, I created a new UserControl MyUserControl.

Now I am surprised: the UserContol does not have a location.

How can I read (by code) myUserControl1.Location in the parent container?

I explain:

I have some Dots (UserControls) that the user can drag in a panel. Actually, I am not sure what kind of Panel this will be... Perhaps Grid.

Now, these dots should be linked with a Line.

Actually, I have a Dot.Head and Dot.Queue properties (also Dots). So, when a Head or Queue is added, I need to dinamically create a link (Line) between them [A]-----[B]. This for this Line I search the Start and End points to set.

Control 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>

Code:

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);
        }
    }
}

解决方案

The WPF layout system doesn't use absolute positioning, unless you're placing your controls on a container that supports absolute positioning (typically a Canvas). If you're using a Canvas, you can get or set the position of the control using the Canvas.Left, Canvas.Right, Canvas.Top and Canvas.Bottom attached properties:

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

Now, if you want the actual location of the control (relative to its parent), you can use the VisualTreeHelper.GetOffset method:

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

这篇关于用户控件的位置在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆