如何像在网页中一样在 WPF 中展开和折叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20605914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:13:26  来源:igfitidea点击:

How to expand and collapse in WPF like in webpage

c#wpf

提问by Offer

Is anyone familiar with websites that have an attribute similar to a tree-view? Like the download segment of the Microsoft website. You press the plus button, it expands and everything below it moves further down. You press the minus button and everything in that block collapses and the content below shifts back up.

有没有人熟悉具有类似于树视图的属性的网站?像微软网站的下载部分。你按下加号按钮,它会展开,它下面的所有东西都进一步向下移动。您按下减号按钮,该块中的所有内容都会折叠起来,下面的内容会向上移动。

Granted C# is nothing like HTML and CSS but I just wanted to know if it was possible to do the same in a WPF application.

当然,C# 与 HTML 和 CSS 完全不同,但我只是想知道是否可以在 WPF 应用程序中执行相同的操作。

It seems like the tree-view currently in the tool box allows for text only to be implemented. It doesn't allow for additional objects such as labels or text-boxes.

似乎工具箱中当前的树视图只允许实现文本。它不允许使用其他对象,例如标签或文本框。

I discovered the EXPANDER and it does a good job of expanding and collapsing its content's but isn't quite capable of pulling objects beneath it back up or pushing them back down. Here's an example of the scenario I would like.

我发现了 EXPANDER,它在扩展和折叠其内容方面做得很好,但不太能够将其下方的对象拉回或推回。这是我想要的场景的示例。

exibitAexibitB

展品A展品B

An example of what I'm going for would be microsoft's download pageif it helps. How their expand and collapse buttons work.

如果有帮助,我将要寻找的一个例子是微软的下载页面。他们的展开和折叠按钮如何工作。

So is there any way to do this?

那么有没有办法做到这一点?

回答by Default

Here is an example of using the Expanderas the way the download page on Microsoft uses it. Note that the Heightof the RowDefinitionsis set to Auto, otherwise the Expanderdoes not collapse when IsExpandedis set to false.

这是一个使用 的示例,Expander作为 Microsoft 上的下载页面使用它的方式。请注意,HeightRowDefinitions设置为Auto,否则Expander不会崩溃时IsExpanded设置为false。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Expander IsExpanded="True">
        <Border BorderBrush="Red" BorderThickness="2">
            <TextBlock Height="50" Text="Hello" />
        </Border>
    </Expander>
    <Expander Grid.Row="1" IsExpanded="True">
        <Border BorderBrush="Green" BorderThickness="2">
            <TextBlock Height="50" Text="World" />
        </Border>
    </Expander>
</Grid>

回答by Nahum

regular tree view can do what you ask.

常规树视图可以满足您的要求。

see this wonderful code-project explanation:

看到这个精彩的代码项目解释:

http://www.codeproject.com/Articles/124644/Basic-Understanding-of-Tree-View-in-WPF

http://www.codeproject.com/Articles/124644/Basic-Understanding-of-Tree-View-in-WPF

回答by Felice Pollano

WPF Expandercomponent do exactly what you want and it push down other control if hosted in a proper panel. Try using a StackPanelfor example.

WPFExpander组件完全符合您的要求,如果托管在适当的面板中,它会下推其他控件。尝试使用StackPanel例如。