Wpf 通过 .X 和 .Y 更改控制位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17845594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:16:45  来源:igfitidea点击:

Wpf change control location via .X and .Y

c#wpf

提问by Vlad Vlad

Since I first start using WPF my problem was how to change a control location and I couldn't find anything beside .Margin = new Thickness(...), the problem is that this function isn't like the old .X and .Y.

自从我第一次开始使用 WPF 以来,我的问题是如何更改控件位置而我在旁边找不到任何东西.Margin = new Thickness(...),问题是这个函数不像旧的 .X 和 .Y。

Is there any way to change a control location on the X and Y axis in place of the old Thicnkess function ? Also I want this by code because my buttons are created dynamically.

有什么方法可以改变 X 和 Y 轴上的控制位置来代替旧的 Thicnkess 函数吗?我也希望通过代码实现这一点,因为我的按钮是动态创建的。

Example: If I have a button called mybutton how do I change his location like this:

示例:如果我有一个名为 mybutton 的按钮,我该如何更改他的位置,如下所示:

mybutton.X=...;
mybutton.Y=...;

回答by sa_ddam213

The Canvaselement is the only layout control that I can think of that uses X and Y coordinates

Canvas元素是唯一的布局控制,我能想到的是使用X和Y坐标

Example:

例子:

Button button = new Button { Content = "StackOverflow" };

Canvas canvas = new Canvas();
canvas.Children.Add(button);
Canvas.SetLeft(button, 100); //X
Canvas.SetTop(button, 10); //Y