How to create a collapsible panel in WPF

青春壹個敷衍的年華 提交于 2019-11-30 14:40:11

问题


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

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

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


回答1:


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

Expander Class

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




回答2:


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

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



来源:https://stackoverflow.com/questions/1305065/how-to-create-a-collapsible-panel-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!