在WPF窗口中按x,y坐标设置用户控件位置。 [英] Set user control location by x,y coordinates in WPF window.

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

问题描述

我一直在检查如何设置动态生成的多个用户控件的位置。



有些链接谈论使用TransformToAncestor,但所有示例仅解释了如何用它来获取当前控制位置,而不是在创建控件时设置它。



其他链接建议将它们插入画布并使用Canvas属性(Canvas。左,Canvas.Top,...)。我做了一个小测试,这对我不起作用:



测试testcontrol = 测试(卡车司机); 
int left = 5 ;
int top = 5 ;
for int y = 0 ; y < 5 ; y ++)
{
Canvas.SetLeft (testcontrol,left); // 设置小部件位置
Canvas.SetTop(testcontrol,top); // 设置小部件位置
.c1 .Children.Add(T);
left + = 5 * 5 ;
top + = 5 * 5 ;
}





基本上我试图在我的c1画布上生成5个测试控件和分配的增量位置。并且不起作用。



此解决方案也不符合我的目标。我想将测试控件的位置(在代码后面动态生成)与X,Y值绑定。

是否有更合适的解决方案来定位x,y值的用户控件?

解决方案

这是你做的:你创建一个控件实例;它的引用是 testcontrol 。然后你在5个不同的位置在画布上移动它。难怪,第一步的结果被遗忘,你将控制权留在最后位置。只有最后一个(左下)位置计数并显示。



你会期待什么?



一行不清楚:

  .c1.Children.Add(t); 



由于 t 定义未显示在您的代码示例中,我不知道它是什么;甚至不知道这段代码是否编译。如果它编译,这是一个变量或在其他地方声明的成员。无论如何,在逻辑树中向同一个父元素添加一些 UIElement 应该不起作用...



-SA


我找到了解决方案,非常简单,我可以使用margin属性设置控件内部的位置。这是WPF当前的等价物。



所以在Test类中,在构造函数中我可以声明:

this.Margin = new Thickness (X,Y,0,0);



并在此处绑定myvalues。



我想它检查上一个问题:



控制位置在wpf中 [ ^ ]

I have been checking how to set the position of multiple user controls generated dynamically.

Some links talk about use "TransformToAncestor" but all the examples only explain how to get the current control location with it , instead of set it when you create the control.

Other links recommend insert them into a canvas and use Canvas properties ( Canvas.Left , Canvas.Top,...). I did a small test and this is not working for me:

Test testcontrol = new Test(trucker);
               int left = 5;
               int top = 5;
               for (int y = 0; y < 5; y++)
               {
                   Canvas.SetLeft(testcontrol, left); //set widget position
                   Canvas.SetTop(testcontrol, top);//set widget position
                   this.c1.Children.Add(t);
                   left += 5 * 5;
                   top += 5 * 5;
               }



Basically I was trying to generate on my c1 canvas 5 test controls and at incremental positions assigned. And doesn't works.

Also this solution doesn't fit with my objective very well. I would like to bind the position of the test controls ( generated dynamically in code behind) with X,Y values.
Is there more properly solution for locate user controls by x,y values?

解决方案

Here is what you do: you create a single instance of control; its reference is testcontrol. Then you move it on canvas in 5 different positions. No wonder, the result of first moves is forgotten, you leave the control in the last position. Only the last (left bottom) position counts and will be shown.

What would you expect?

One line is unclear:

this.c1.Children.Add(t);


As t definition is not shown in your code sample, I don't know what is it; and don't even know if this code compiles. If it compiles, this is a variable or a member declared somewhere else. Anyway, adding some UIElement to the same parent element in logical tree should not work…

—SA


I found the solution , very easy , I can set the position inside of the control using margin property. It is the current equivalent in WPF.

So at Test class , in the constructor I can declare :
this.Margin = new Thickness(X, Y, 0, 0);

and bind here myvalues as I prefer.

I figure it checking this previous question:

control position in wpf[^]


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

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