gpt4 book ai didi

c# - WPF Expander - 当 GridSplitter 用于手动调整行大小时,行未正确折叠

转载 作者:行者123 更新时间:2023-11-30 22:01:00 26 4
gpt4 key购买 nike

这是我的

   <Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="25"/>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>

<Expander IsExpanded="False" Grid.Row="0" >
<DataGrid name="FirstGrid" />
</Expander>
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" Height="5" />
<DataGrid Grid.Row="2" name="SecondGrid" />

当我单击扩展器上的展开按钮时,它会正确地将第 0 行展开为 DataGrid FirstGrid 的大小,并且还会正确折叠该行。但是,如果我展开 FirstGrid 然后通过向上或向下拖动 GridSplitter 然后按展开器上的折叠按钮手动调整该行的大小,这将不起作用。发生的情况是 FirstGrid 折叠但 FirstGrid 所在的行本身没有折叠。有什么建议吗?

谢谢

最佳答案

Rob 是对的,一旦移动 gridsplitter,第一行的高度将不再是 Auto,因此它不会响应扩展器大小的变化为了使其工作,您需要向扩展器添加一个行为,该行为将监听 expander.expanded 和 collapsed 事件并将网格行再次更新为自动。像这样:

public class GridColumnWidthReseterBehaviour : Behavior<Expander>
{
private Grid _parentGrid;
public int TargetGridRowIndex { get; set; }
protected override void OnAttached()
{
AssociatedObject.Expanded += AssociatedObject_Expanded;
AssociatedObject.Collapsed += AssociatedObject_Collapsed;
FindParentGrid();
}

private void FindParentGrid()
{
DependencyObject parent = LogicalTreeHelper.GetParent(AssociatedObject);
while (parent != null)
{
if (parent is Grid)
{
_parentGrid = parent as Grid;
return;
}
parent = LogicalTreeHelper.GetParent(AssociatedObject);
}
}

void AssociatedObject_Collapsed(object sender, System.Windows.RoutedEventArgs e)
{
_parentGrid.RowDefinitions[TargetGridRowIndex].Height= GridLength.Auto;
}

void AssociatedObject_Expanded(object sender, System.Windows.RoutedEventArgs e)
{
_parentGrid.RowDefinitions[TargetGridRowIndex].Height= GridLength.Auto;
}
}

然后你像这样使用它:

 <Expander ...>
<interactivity:Interaction.Behaviors>
<behaviors:GridColumnWidthReseterBehaviour TargetGridRowIndex="0"></behaviors:GridColumnWidthReseterBehaviour>
</interactivity:Interaction.Behaviors>

...

关于c# - WPF Expander - 当 GridSplitter 用于手动调整行大小时,行未正确折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28095177/

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