为什么在堆栈面板的排列覆盖中不考虑ZIndex [英] Why ZIndex is not considered in arrange override of a stackpanel

查看:81
本文介绍了为什么在堆栈面板的排列覆盖中不考虑ZIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要尝试的操作:

  1. 在我的窗口中添加了一个面板(方向:水平)
  2. 为其添加了一组按钮
  3. 将第一个按钮的ZIndex设置为高于第二个按钮
  4. 增加第一个按钮的宽度

我的期望:

  1. 我希望第一个按钮位于第二个按钮的上方(至少覆盖)
  2. 除非第一个按钮的宽度不足,否则StackPanel的宽度不应更改

实际发生的事情:

  1. 第一个按钮的宽度增加,第二个按钮相应地向右移动.他们呆在同一架飞机上
  2. StackPanel的宽度随着第一个按钮的宽度增加而增加

这是我的问题:

我知道stackpanel在自己安排项目时没有考虑ZIndex,为什么?它在安排孩子时不应该考虑其孩子的ZIndex吗?

I know that stackpanel has not considered ZIndex while arranging the items within itself, BUT WHY?? Shouldn't it consider the ZIndex of its children while arranging them???

推荐答案

Stackpanel根据其宽度堆叠"其子级,即,如果增加项目的宽度(或增加其边距),则Stackpanel会简单地扩展为了适应这一点.如果要强制堆叠面板中的项目重叠,则必须在计算布局后更改其位置.您可以使用RenderTransform执行此操作.请参见下面的示例:

The Stackpanel 'stacks' its children based on their widths, i.e. if you increase the width of an item (or increase its margin), the stackpanel will simply expand to accomodate this. If you want to force items within a stackpanel to overlap, you will have to change their location after the layout has been computed. You can perform this using a RenderTransform. See the example below:

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
  <Button Content="One" Canvas.ZIndex="10">
    <Button.RenderTransform>
      <TranslateTransform X="10"/>
    </Button.RenderTransform>
  </Button>
  <Button Content="One"/>
  <Button Content="One"/>
  <Button Content="One"/>
</StackPanel>

是的,ZIndex被尊重.这是Canvas的附带内容,但是,它似乎由渲染引擎直接使用,而不是由Canvas使用,因此可以在上面的代码中使用.

And yes, the ZIndex is respected. This is an attached proepry of Canvas, however, it seems to be used by the rendering engine directly rather than by Canvas, hence it works in the above code.

这篇关于为什么在堆栈面板的排列覆盖中不考虑ZIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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