Click here to Skip to main content
15,881,092 members
Articles / Desktop Programming / XAML
Tip/Trick

A Foldable Content Control for UWP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Jan 2017CPOL1 min read 6.7K   3   1
This a foldable content control for UWP platform.

Introduction

This is a Content Control which you can fold and expand by tapping on its header.

Image 1

Background

One of the last projects I did was coding up the design of an enterprise app. And like in most enterprise apps, this app had a list which needs to be folded and expanded. But unfortunately, there was no such out of the box control in UWP (like we had in WPF). So I had to create one from scratch. And with the time limitation in mind, I decided to go with a User Control. Guess what, the final outcome was not too bad. So I decided to share it with the community.

Using the Code

You can use this control with any kind of framework element. It’s not just limited to a list. What you have to do is simply assign the property ‘FoldableContent’ to the content you want. Like below:

XML
<controls:FoldableContentControl.FoldableContent>
    <ListView ItemsSource="{x:Bind MyItemList}">
        <ListView.ItemTemplate>
            <DataTemplate>
               <StackPanel BorderBrush="LightGray"
                        BorderThickness="0.5">
                    <TextBlock Text="{Binding}"
                           HorizontalAlignment="Center"
                           Padding="10"/>
               </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter  Property="Padding" Value="0"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</controls:FoldableContentControl.FoldableContent>

You can specify the content in the header from changing the ‘HeaderText’ property.

You can specify the content you want to display when there are no items in the content. Simply add a framework element to the ‘EmptyContent’ property or change the ‘EmptyText’ property. To display this emptiness, you have to set the ‘DisplayEmpty’ property to true (if an ItemsControl simply binds the ‘Count’ property with a value converter).

Points of Interest

You can find the source code in my GitHub repo.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Sri Lanka Sri Lanka
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ammar Shaukat22-Aug-17 1:48
professionalAmmar Shaukat22-Aug-17 1:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.