您如何在WPF中进行相对定位? [英] How do you do relative positioning in WPF?

查看:355
本文介绍了您如何在WPF中进行相对定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WPF中相对定位元素?
标准模型是对所有内容都使用布局管理器,但是如果只想根据其他元素的位置来放置元素(例如,在Canvas上)怎么办?



例如,您可能希望将一个元素(例如一个按钮)附加到另一个元素(可能是面板)的侧面,而与该面板的位置或布局无关。
任何使用过工程工具(SolidWorks,AutoCad等)的人都熟悉这种相对位置。



将所有内容强制放入布局管理器中(不同WPF面板)在某些情况下没有多大意义,在这种情况下,您不必关心元素是否由某个父容器维护,并且您不希望其他子容器受到彼此布局/外观的更改的影响。 WPF是否以任何方式支持此相对定位模型?在画布上,您可以将一个堆栈面板水平放置在画布上,然后在其中放置两个按钮。



就像这样:

 < Canvas> 
< StackPanel Canvas.Left = 100 Canvas.Top = 100 Orientation = Horizo​​ntal>
< Button>按钮1< / Button>< Button>按钮2< / Button>
< / StackPanel>
< / Canvas>



我认为当您在表单中使用多个布局时,它非常灵活,您可以创建漂亮的很多您想要的配置。


How can you relatively position elements in WPF? The standard model is to use layout managers for everything, but what if you want to position elements (on a Canvas, for example) simply based on the position of other elements?

For example, you may want one element (say a button) to be attached the side of another (perhaps a panel) independent of the position or layout of that panel. Anyone that's worked with engineering tools (SolidWorks, AutoCad, etc.) is familiar with this sort of relative positioning.

Forcing everything into layout managers (the different WPF Panels) does not make much sense for certain scenarios, where you don't care that elements are maintained by some parent container and you do not want the other children to be affected by a change in the layout/appearance of each other. Does WPF support this relative positioning model in any way?

解决方案

Instead of putting (as in your example) a button directly on the canvas, you could put a stackpanel on the canvas, horizontally aligned, and put the two buttons in there.

Like so:

<Canvas>
  <StackPanel Canvas.Left="100" Canvas.Top="100" Orientation="Horizontal">
    <Button>Button 1</Button><Button>Button 2</Button>
  </StackPanel>
</Canvas>

I think that it's quite flexible when you use more than 1 layout in a form, and you can create pretty much any configuration you want.

这篇关于您如何在WPF中进行相对定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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