How do you do relative positioning in WPF?

笑着哭i 提交于 2019-11-30 01:31:28
Dave Arkell

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.

Good question. As far as I know, we need to have a different custom panel to get this feature. Since WPF is based on Visual Hierarchy there is no way to have this sort of Flat structure for the elements in the platform.

But Here is a trick to do this. Place your elements in the same position and give relative displacement by using RenderTransform.TranslateTransform. This way your TranslateTransfrom's X and Y will always be relatuve to the other element.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!