如何在 WPF 中创建可折叠面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1305065/
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-08 20:53:06  来源:igfitidea点击:

How to create a collapsible panel in WPF

wpfpanelcollapsiblepanelextender

提问by user301016

I am creating a Windows application (WPF) and C#. In my view, I have to add few layouts like browsing a folder, displaying the files in the folder in a list view...etc

我正在创建一个 Windows 应用程序 (WPF) 和 C#。在我看来,我必须添加一些布局,例如浏览文件夹、在列表视图中显示文件夹中的文件...等

My requirement is : The panels mentioned above should be collapsible panels, I guess, we dont have option of collapsible panel in wpf.

我的要求是:上面提到的面板应该是可折叠面板,我想,我们在 wpf 中没有可折叠面板的选项。

I have to create a custom control for this? If so, Please suggest me how to do this?

我必须为此创建一个自定义控件?如果是这样,请建议我如何做到这一点?

回答by Bojan Resnik

The Expander control may be what you are looking for. From MSDN:

Expander 控件可能正是您要找的。从MSDN

Expander Class

Represents the control that displays a header that has a collapsible window that displays content.

扩展器类

表示显示标题的控件,该标题具有显示内容的可折叠窗口。

回答by user2821937

May like this?

可以这样吗?

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>
    <Border  Background="Red" Height="12" VerticalAlignment="Top" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave"></Border>
</Grid>    

C# code behind

后面的 C# 代码

 private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
    {
        Border sp = sender as Border;
        DoubleAnimation db = new DoubleAnimation();
        //db.From = 12;
        db.To = 150;
        db.Duration = TimeSpan.FromSeconds(0.5);
        db.AutoReverse = false;
        db.RepeatBehavior = new RepeatBehavior(1);
        sp.BeginAnimation(StackPanel.HeightProperty, db);
    }

    private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
    {
        Border sp = sender as Border;
        DoubleAnimation db = new DoubleAnimation();
        //db.From = 12;
        db.To = 12;
        db.Duration = TimeSpan.FromSeconds(0.5);
        db.AutoReverse = false;
        db.RepeatBehavior = new RepeatBehavior(1);
        sp.BeginAnimation(StackPanel.HeightProperty, db);
    }
}

You can use any element control like grid, stack, dock, border ...

您可以使用任何元素控件,如网格、堆栈、停靠栏、边框...