简体   繁体   English

如何根据页眉高度定位WPF扩展器?

[英]How to position WPF Expander based on Header height?

I have been struggling with a problem that at first glance might look easy for quite some time now. 我一直在困扰着一个问题,乍一看现在可能看起来很容易了。 The problem in short is that I want to position an expander based on the height of the header in the expander. 简而言之,问题是我想根据扩展器中标头的高度来放置扩展器。

The long story is that I have a grid with (currently, but that could be easily changed) two rows. 总而言之,我有一个带有两行的网格(当前,但是可以很容易地更改)。 At the bottom at the first row (kind of a header) I would like to put an Expander. 在第一行的底部(标题的种类),我想放置一个Expander。 However, I would like this expander to expand onto the second row. 但是,我希望此扩展器可以扩展到第二行。 Simply putting the expander in the first row and setting vertical alignment to bottom positions my expander correctly but brakes the expansion (expands to same row only). 只需将扩展器放在第一行并将垂直对齐设置到底部位置,我的扩展器就可以正确放置,但是会阻止扩展(仅扩展到同一行)。 Setting Grid.RowSpan="2" makes my expander expand correctly but that brakes my original vertical alignment to the bottom. 设置Grid.RowSpan =“ 2”可使我的扩展器正确扩展,但这会使我的原始垂直对齐制动到底部。 I've tried putting the expander in the second row and then bind the margin of the expander to the ActualHeight of the expander (along with a converter which gave me a negative top margin equal to the height) which turned out great, until I clicked my expander :-). 我尝试将扩展器放到第二行,然后将扩展器的边距绑定到扩展器的ActualHeight(以及一个转换器,该转换器为我提供了一个等于高度的负顶部余量),结果变好,直到单击为止我的扩展器:-)。 Setting the binding mode to "OneTime" gave me a zero margin for some reason. 出于某些原因,将绑定模式设置为“ OneTime”使我的利润为零。

Do anybody have an easy solution for this? 有人对此有一个简单的解决方案吗? Would it help to create a control template for my expander based on a canvas (my thinking is that canvas would be drawn on top of other controls)? 为基于画布的扩展器创建控件模板是否有帮助(我的想法是将画布绘制在其他控件之上)? I guess I could have done databinding to a descendant control if that was supported (which I don't think it is). 我想我可以完成对子孙控件的数据绑定(如果支持的话)(我认为不是)。 Have I missed any obvious solutions? 我错过了任何明显的解决方案吗?

I have the possibilty to change the control template for the expander and changing the grid layout of my window containing the control is not an issue if that could help me in anyway. 我可以更改扩展器的控件模板,并且无论如何都可以改变包含控件的窗口的网格布局,这不是问题。

I am running Visual Studio 2008, .NET 3.5. 我正在运行Visual Studio 2008,.NET 3.5。

//daniel //丹尼尔

There is a way to make it appear that a single expander has separate layout for its header and content, so they can be in separate grid cells or even totally separate parts of the window. 有一种方法可以使单个扩展器的标题和内容具有不同的布局,因此它们可以位于单独的网格单元中,甚至可以位于窗口的完全分开的部分中。 This may be overkill for what you really want to accomplish, but here it is: 对于您真正想要实现的目标来说,这可能是过高的,但是在这里是:

  1. Create an expander with your header but an empty body. 用标头创建一个扩展器,但正文为空。 Place it in the upper cell. 将其放在上部单元格中。
  2. Create another expander with the body you want but set its Template property to a template containing just a ContentPresenter so no header will be visible. 使用所需的主体创建另一个扩展器,但是将其Template属性设置为仅包含ContentPresenter的模板,因此不会显示任何标题。 Place it in the lower cell. 将其放在下部单元格中。
  3. Bind the second expander's IsExpanded property to the first expander's IsExpanded property 将第二个扩展器的IsExpanded属性绑定到第一个扩展器的IsExpanded属性
  4. Optionally adjust tab order so the two Expanders are together (可选)调整制表符顺序,以使两个扩展器在一起

Now when you expand the upper expander the lower expander will expand as well. 现在,当您扩展上部扩展器时,下部扩展器也会扩展。 If there is no gap between them, they will appear as if they were a single expander. 如果它们之间没有缝隙,它们将看起来像是单个扩展器。

Another option which may be effective is to use a single Expander and modify its template so its entire header (including the button) has VerticalAlignment="Bottom" and is inside a zero-size Canvas. 另一个有效的选择是使用单个Expander并修改其模板,以便其整个标头(包括按钮)具有VerticalAlignment =“ Bottom”且位于零尺寸的Canvas内。 In that case the entire Expander would go in your lower cell. 在这种情况下,整个扩展器将进入您的下部单元格。 This has the disadvantage that layout of the upper cell would be unaware of the space used by the Expander so it would likely place other controls over or under it depending on the z-order. 这样做的缺点是,上部单元的布局不会知道扩展器使用的空间,因此可能会根据z顺序在其上方或下方放置其他控件。

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

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