网格扩展器 [英] Expanders in Grid
问题描述
毫无疑问,这将是直截了当的,但是无论出于何种原因,我的想法都在此空白.
This is going to be straight forward no doubt, but for what ever reason, my mind is drawing a blank on it.
我有一个不可调整大小的小窗口(325x450),其中有3个扩展器,垂直堆叠.每个扩展器都包含一个ItemsControl,该控件可能包含很多项目,因此需要滚动.
I've got a small, non-resizeable window (325x450) which has 3 Expanders in it, stacked vertically. Each Expander contains an ItemsControl that can potentially have a lot of items in and therefore need to scroll.
我似乎无法正确理解的是如何布置扩展器,以使其扩展以填充可用的任何空间,而无需将其他元素推离屏幕.我可以通过使用Grid并将每个扩展器以*高度排成一行来达到我想要的目的,但是这意味着它们始终占据每个窗口的1/3,这会破坏扩展器的位置:)
What I can't seem to get right is how to layout the Expanders so that they expand to fill any space that is available without pushing other elements off the screen. I can sort of achieve what I'm after by using a Grid and putting each expander in a row with a * height, but this means they are always taking up 1/3 of the window each which defeats the point of the Expander :)
我想要实现的胡扯图:
推荐答案
如果您不介意任何代码隐藏,则可能会陷入Expanded
/Collapsed
事件,找到父级Grid
,获取扩展器的RowDefinition
,并将其值设置为等于*
(如果已扩展),或者等于Auto
.
If you don't mind a little code-behind, you could probably hook into the Expanded
/Collapsed
events, find the parent Grid
, get the RowDefinition
for the expander, and set the value equal to *
if its expanded, or Auto
if not.
例如,
Expander ex = sender as Expander;
Grid parent = FindAncestor<Grid>(ex);
int rowIndex = Grid.GetRow(ex);
if (parent.RowDefinitions.Count > rowIndex && rowIndex >= 0)
parent.RowDefinitions[rowIndex].Height =
(ex.IsExpanded ? new GridLength(1, GridUnitType.Star) : GridLength.Auto);
FindAncestor
方法的定义如下:
public static T FindAncestor<T>(DependencyObject current)
where T : DependencyObject
{
// Need this call to avoid returning current object if it is the
// same type as parent we are looking for
current = VisualTreeHelper.GetParent(current);
while (current != null)
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}
这篇关于网格扩展器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!