How to create a collapsible panel in WPF

2019-02-07 22:23发布

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?

2条回答
Juvenile、少年°
2楼-- · 2019-02-07 22:30

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

查看更多
太酷不给撩
3楼-- · 2019-02-07 22:39

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.

查看更多
登录 后发表回答