简体   繁体   English

C# WPF 如何以编程方式设置控件的位置、宽度和高度?

[英]C# WPF how to set location,width and height of the controls programmatically?

I'm doing my first WPF application.我正在做我的第一个 WPF 应用程序。 im having problem whereby when my form is maximized or fullscreen, my controls wont resize and just stay in the same location.我有一个问题,当我的表单最大化或全屏时,我的控件不会调整大小而只是停留在同一位置。 only the form is maximized.只有形式最大化。

in winform, I can do the adjustment in the .cs like the following:在 winform 中,我可以在 .cs 中进行如下调整:

panel6.Width
panel6.Height
panel6.Location

this will help me set it if my form is maximized.如果我的表单最大化,这将帮助我设置它。 I do it by using some arithmetics where I get the resolution of screen and some calculation and get the value and can set it to the width, height and location.我通过使用一些算法来获得屏幕分辨率和一些计算并获得值并可以将其设置为宽度、高度和位置。 BUT THIS IS IN WINFORM.但这是在 WINFORM 中。

how will I tackle this issue for maximized and fullscreen in WPF?我将如何解决 WPF 中的最大化和全屏问题? is there a way to be done through the .cs file programmatically?有没有办法以编程方式通过 .cs 文件完成? or does WPF come with a easy built in control to tackle this issue?或者 WPF 是否带有一个简单的内置控件来解决这个问题?

suppose for this example I'm using dockpanel in the WPF.假设对于这个例子,我在 WPF 中使用了dockpanel。

it will be pointless if window is maximized but the other controls remains.如果窗口最大化但其他控件仍然存在,那将毫无意义。

To set Width and Height :设置宽度高度

dockpanel1.width = 230;
dockpanel1.height = 230;

as for location, wpf uses Margin :至于位置, wpf使用Margin

dockpanel1.Margin = new Thickness(0,440,836,40);

It is possible to programmatically move child elements on a Canvas.可以通过编程方式移动 Canvas 上的子元素。

In xaml:在 xaml 中:

<Canvas>
    <YourElement Canvas.Top="x" Canvas.Left="y"/>
</Canvas>

In C#:在 C# 中:

Canvas.SetTop(YourElement, newX);
Canvas.SetLeft(YourElement, newY);

Use some calculations like (control's previous position * layout's new size) / layout's previous size = control's new position使用一些计算,例如(控件的先前位置 * 布局的新大小)/布局的先前大小 = 控件的新位置

But the easiest way is to use XAML Use Grid and put columns and rows in it and set the size of columns and rows to * So on layout size change, your controls will reposition in refer to parent's change in size which your grid is child of it.但最简单的方法是使用 XAML Use Grid 并在其中放置列和行,并将列和行的大小设置为 * 因此,在布局大小更改时,您的控件将重新定位参考您的网格是其子项的父项的大小更改它。 And you could even have auto resizable controls just by setting the controls' margins in columns and rows.您甚至可以通过在列和行中设置控件的边距来拥有自动调整大小的控件。 Don't forget horizontal and vertical alignments set to stretch.不要忘记设置为拉伸的水平和垂直对齐方式。

For your specific problem, would recommend you to use a DockPanel and place your controls in it.对于您的特定问题,建议您使用DockPanel并将您的控件放在其中。 Here's first bing result: WPF Tutorial |这是第一个 bing 结果: WPF 教程 | Dock Panel码头面板

And as already suggested by flq and blindmeis, do study the layout panels.正如 flq 和 blindmeis 已经建议的那样,请研究布局面板。 That will make your life really simpler in WPF.这将使您在 WPF 中的生活变得更加简单。

you should look at all the different layoutpanels in wpf (Grid, DockPanel, StackPanel, Canvas ...)您应该查看 wpf 中所有不同的布局面板(Grid、DockPanel、StackPanel、Canvas ...)

so if you set any properties for your Panel, it behaves different if your LayoutPanel change.因此,如果您为 Panel 设置任何属性,则如果您的 LayoutPanel 更改,它的行为会有所不同。

If you want explicitly position your child items use Canvas panel如果您想明确定位您的子项目,请使用 Canvas 面板

<Canvas>
    <YourElement Canvas.Top="x" Canvas.Left="y"/>
</Canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM