gpt4 book ai didi

wpf - 网格中的扩展器

转载 作者:行者123 更新时间:2023-12-02 00:44:25 24 4
gpt4 key购买 nike

毫无疑问,这将是直截了当的,但无论出于何种原因,我的大脑一片空白。

我有一个不可调整大小的小窗口 (325x450),其中有 3 个扩展器,垂直堆叠。每个 Expander 都包含一个 ItemsControl,其中可能包含大量项目,因此需要滚动。

我似乎无法理解的是如何布局扩展器,以便它们扩展以填充任何可用空间,而不会将其他元素推离屏幕。我可以通过使用网格并将每个扩展器放在具有 * 高度的一行中来实现我想要的目标,但这意味着它们总是占用每个窗口的 1/3,这违背了扩展器的要点:)

我想要实现的目标的蹩脚图表:

enter image description here

最佳答案

这个要求有点不寻常,因为您希望 Grid 中的 Children 的状态来决定 Height >RowDefinition 它们位于。
我真的很喜欢这个布局想法,我不敢相信我自己从来没有类似的要求..:)

对于可重用的解决方案,我将使用网格的附加行为。
该行为将订阅附加事件 Expander.ExpandedExpander.Collapsed 并在事件处理程序中,从 获取正确的 RowDefinition Grid.GetRow 并相应地更新Height。它的工作原理是这样的

<Grid ex:GridExpanderSizeBehavior.SizeRowsToExpanderState="True">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Expander Grid.Row="0" ... />
<Expander Grid.Row="1" ... />
<Expander Grid.Row="2" ... />
<!-- ... -->
</Grid>

这是GridExpanderSizeBehavior

public class GridExpanderSizeBehavior
{
public static DependencyProperty SizeRowsToExpanderStateProperty =
DependencyProperty.RegisterAttached("SizeRowsToExpanderState",
typeof(bool),
typeof(GridExpanderSizeBehavior),
new FrameworkPropertyMetadata(false, SizeRowsToExpanderStateChanged));
public static void SetSizeRowsToExpanderState(Grid grid, bool value)
{
grid.SetValue(SizeRowsToExpanderStateProperty, value);
}
private static void SizeRowsToExpanderStateChanged(object target, DependencyPropertyChangedEventArgs e)
{
Grid grid = target as Grid;
if (grid != null)
{
if ((bool)e.NewValue == true)
{
grid.AddHandler(Expander.ExpandedEvent, new RoutedEventHandler(Expander_Expanded));
grid.AddHandler(Expander.CollapsedEvent, new RoutedEventHandler(Expander_Collapsed));
}
else if ((bool)e.OldValue == true)
{
grid.RemoveHandler(Expander.ExpandedEvent, new RoutedEventHandler(Expander_Expanded));
grid.RemoveHandler(Expander.CollapsedEvent, new RoutedEventHandler(Expander_Collapsed));
}
}
}
private static void Expander_Expanded(object sender, RoutedEventArgs e)
{
Grid grid = sender as Grid;
Expander expander = e.OriginalSource as Expander;
int row = Grid.GetRow(expander);
if (row <= grid.RowDefinitions.Count)
{
grid.RowDefinitions[row].Height = new GridLength(1.0, GridUnitType.Star);
}
}
private static void Expander_Collapsed(object sender, RoutedEventArgs e)
{
Grid grid = sender as Grid;
Expander expander = e.OriginalSource as Expander;
int row = Grid.GetRow(expander);
if (row <= grid.RowDefinitions.Count)
{
grid.RowDefinitions[row].Height = new GridLength(1.0, GridUnitType.Auto);
}
}
}

关于wpf - 网格中的扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com