将 Expander 重置为默认折叠行为

发布于 2024-07-07 03:04:52 字数 617 浏览 11 评论 0原文

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

我该如何说服 Expander 再次正确崩溃? 或者修改 Resizer 不让 Expander 伤心也可以。

扩展器文档说:

“为使 Expander 正常工作,当 ExpandDirection 属性设置为 Down 或 Up 时,不要在 Expander 控件上指定 Height。同样,当 ExpandDirection 属性设置为 Left 时,不要在 Expander 控件上指定 Width当您在 Expander 控件上设置展开内容的显示方向的大小时,即使窗口折叠,也会显示由大小参数定义的区域。设置展开窗口的大小,设置 Expander 控件的内容或包含该内容的 ScrollViewer 的大小尺寸。”

I'm using an expander inside a Resizer (a ContentControl with a resize gripper), and it expands/collapses properly when the control initially comes up. Once I resize it, the Expander won't properly collapse, as documented below. I ran Snoop on my application, and I don't see any heights set on Expander or its constituents.

How would I go about convincing Expander to collapse properly again? Or modifying Resizer to not make Expander sad would work as well.

Expander documentation says:

"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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-07-14 03:04:52

我通过将 Resizer 移动到 Expander 内解决了这个问题,但我在其他地方遇到了 Expander 问题,所以如果有人有答案,我仍然希望得到答案。

谢谢

I resolved the problem by moving the Resizer inside the Expander, but I've run into the Expander issue elsewhere, so would still like an answer if someone has it.

thanks

浅黛梨妆こ 2024-07-14 03:04:52

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

讽刺的是,这是通过阅读我最初使用的 Resizer 控件的代码得出的。

I haven't had a chance to mock up this particular issue since then, but I recently discovered that setting Height or Width to Double.NaN resets it to its default free-spirited behavior.

Ironically, this was from reading the code of the Resizer control I was using in the first place.

只想待在家 2024-07-14 03:04:52

回答这个问题有点晚了(两年多),但是,嘿,迟到总比不好,对吧?

不管怎样,我遇到了这个确切的问题,并且能够通过一些隐藏代码来保存和重置列宽度来解决它。

我有一个 3 列网格,第一列中有一些内容,第二列中有 GridSplitter,第三列中有扩展器。 看起来发生的情况是,移动 GridSplitter 后,包含 Expander 的列的宽度从“自动”更改为固定大小。 这会导致 Expander 不再按预期折叠。

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

    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>

Answering this a bit late (2+ years), but, hey, better late than never, right?

Anyway, I ran into this exact problem and was able to solve it with some code-behind to save and reset column widths.

I have a 3 columned Grid, with some content in the first column, the GridSplitter in the second column, and the Expander in the third column. It looks like what is happening is that after the GridSplitter is moved the width of the column containing the Expander is altered from Auto to a fixed size. This causes the Expander to no longer collapse as expected.

So, I added a private variable and two event handlers:

    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;
    }

When the Expander is collapsed I save Column2's fixed width (which was altered from Auto auto-magically in the background somewhere) then reset the width to Auto.

Then, when the expander is expanded, I restore the column back to the fixed width so it expands to the same width it was before it was collapsed.

Here's the XAML for reference:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文