如何在 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
How to create a collapsible panel in WPF
提问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
回答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 ...
您可以使用任何元素控件,如网格、堆栈、停靠栏、边框...