将扩展器重置为默认折叠行为

时间:2020-03-06 14:59:44  来源:igfitidea点击:

我在Resizer(具有调整大小的抓取器的ContentControl)内部使用了一个扩展器,当控件最初出现时,它会正确地扩展/折叠。调整大小后,扩展器将无法正确折叠,如下所述。我在应用程序上运行了Snoop,但没有看到Expander或者其组成部分的高度设置。

我该如何说服Expander再次正常崩溃?或者修改Resizer而不会使Expander感到悲伤也可以。

扩展器文档说:

"For an Expander to work correctly, do not specify a Height on the Expander control when the ExpandDirection property is set to Down or Up. Similarly, do not specify a Width on the Expander control when the ExpandDirection property is set to Left or Right. When you set a size on the Expander control in the direction that the expanded content is displayed, the area that is defined by the size parameter is displayed with a border around it. This area displays even when the window is collapsed. To set the size of the expanded window, set size dimensions on the content of the Expander control or the ScrollViewer that encloses the content."

解决方案

我通过将Resizer移到Expander内解决了问题,但是我在其他地方遇到过Expander问题,因此如果有人有,我仍然想要一个答案。

谢谢

我在使用带有GridSplitter的Grid内的Expander时遇到了类似的问题。在我移动分离器之前,expand / collapse行为可以正常工作。...之后,Expander不会折叠,只会隐藏其内容。

我仍在寻找解决方法...最终找到了解决方法吗?

从那时起,我再也没有机会模拟这个特定的问题,但是最近我发现将Height或者Width设置为Double.NaN会将其重置为默认的自由行为。

具有讽刺意味的是,这是从阅读我最初使用的Resizer控件的代码开始的。

回答这个问题有点晚(超过2年),但是,嘿,迟到总比没有好,对吧?

无论如何,我遇到了这个确切的问题,并且能够通过一些隐藏代码的方式来解决该问题,以保存和重置列宽。

我有一个三列的Grid,第一列中包含一些内容,第二列中包含GridSplitter,第三列中包含Expander。看起来正在发生的事情是,在移动GridSplitter之后,包含Expander的列的宽度从Auto更改为固定大小。这将导致扩展器不再按预期崩溃。

因此,我添加了一个私有变量和两个事件处理程序:

private GridLength _columnWidth;

    private void Expander_Expanded (object sender, RoutedEventArgs e)
    {
        // restore column fixed size saved in Collapse event
        Column2.Width = _columnWidth;
    }

    private void Expander_Collapsed (object sender, RoutedEventArgs e)
    {
        // save current column width so we can restore when expander is expanded
        _columnWidth = Column2.Width;

        // reset column width to auto so the expander will collapse properly
        Column2.Width = GridLength.Auto;
    }

当Expander收合时,我保存了Column2的固定宽度(该值在背景中的某个地方从Auto神奇地自动更改了),然后将宽度重置为Auto。

然后,在展开扩展器时,我将列恢复为固定宽度,以使其扩展到与折叠前相同的宽度。

这是供参考的XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition x:Name="Column2" Width="Auto" />
    </Grid.ColumnDefinitions>
    <ScrollViewer Grid.Column="0" VerticalScrollBarVisibility="Auto">
        <!-- some content goes here -->
    </ScrollViewer>
    <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch"
         Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="5"
         Background="Black" />
    <Expander Grid.Column="2" ExpandDirection="Left"
         IsExpanded="True" Style="{StaticResource LeftExpander}"
         Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
        <Grid>
            <TextBox TextWrapping="Wrap" Height="Auto" Margin="0 5 5 5" />
        </Grid>
    </Expander>
</Grid>