Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / WPF

WPF Loading Wait Adorner

Rate me:
Please Sign up or sign in to vote.
4.91/5 (33 votes)
10 Feb 2010CPOL2 min read 229.8K   19K   117   33
Easily add a "Loading" overlay that "grays out" the form and adds a circular progress bar to any form.

Image 1

Introduction

First, let me give credit where credit is clearly due as I am standing on the shoulders of better developers with this one. It's basically a combination of two articles written by others to create something I've always wanted in my WPF toolkit.

The whole AdornedControl project which allows you to define your Adorners in XAML came from Ashley Davis. I'm fairly green when it comes to adorners, and could not have completed my article without his.

The other article I pulled from was Sacha Barber's circular progressbar article. I am not a designer by any stretch of the imagination, and wouldn't have known where to start on an animated circular progress bar.

Using the code

The first thing I did was put together a simple form with a button to simulate starting and stopping loading and a simple ListBox. Then, I wrapped the ListBox with an AdornedControl and created an AdornedControl.AdornerContent which contains the UserControl that will display when the adorner is visible.

XML
<StackPanel>
    <Button Click="Button_Click">Start/Stop Waiting</Button>
    <ac:AdornedControl Name="LoadingAdorner">
        <ac:AdornedControl.AdornerContent>
            <local:LoadingWait></local:LoadingWait>
        </ac:AdornedControl.AdornerContent>
        <ListBox Name="ListBox1">
            <ListBoxItem>Item 1</ListBoxItem>
            <ListBoxItem>Item 2</ListBoxItem>
            <ListBoxItem>Item 3</ListBoxItem>
        </ListBox>
    </ac:AdornedControl>    
</StackPanel>

The Button_Click event calls a method that flips LoadingAdorner.IsAdornerVisible and ListBox1.IsEnabled. In production, those will be bound to the IsEnabled and IsBusy properties on the ViewModel.

C#
private void StartStopWait()
{
    LoadingAdorner.IsAdornerVisible = !LoadingAdorner.IsAdornerVisible;
    ListBox1.IsEnabled = !ListBox1.IsEnabled;
}

<local:LoadingWait> is the UserControl that both "grays out" the adorned area and displays the ring of progress. This is essentially Sacha's code, I just added a brush to the UserControl to do the "graying", and added a brush as a resource so I could easily change the colors of all the dots at once.

Conclusion

I can now go through my existing application and add this little adornment around any area that takes more than an instant to load its data. All I'll have to do is paste the adornment code into the XAML, bind the IsAdornerVisible to an IsBusy property, and set the IsBusy property from code.

As you can see, I didn't add a lot to Ashley and Sacha's previous works, and I would like to thank them again.

History

  • Feb 10th 2010 - Article posted.

License

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


Written By
Software Developer
United States United States
I’m a Software Engineer at Microsoft working on the Azure Portal. Before that I spent about 20 years developed various business applications at a number of different companies. I have a passion for writing clean, scalable code and sharing what I’ve learned with others.

I also help run the Casco Bay .Net User Group

Comments and Discussions

 
QuestionCursor Pin
Timothy Lester18-Oct-18 17:12
Timothy Lester18-Oct-18 17:12 
QuestionFreeze monitor Pin
Enrico Oliva22-Jun-17 22:42
Enrico Oliva22-Jun-17 22:42 
Praisethx Pin
Burak Tunçbilek20-Jan-17 3:47
Burak Tunçbilek20-Jan-17 3:47 
GeneralMy vote of 5 Pin
wwwx26-Jun-16 15:52
wwwx26-Jun-16 15:52 
Questionplease send me this code ok? here is download error (WPF Loading Wait Adorner). Thanks. Pin
Member 1221515112-May-16 3:03
Member 1221515112-May-16 3:03 
AnswerRe: please send me this code ok? here is download error (WPF Loading Wait Adorner). Thanks. Pin
Member 1221515112-May-16 3:04
Member 1221515112-May-16 3:04 
GeneralMy vote of 5 Pin
mikipatate9-Mar-16 4:57
mikipatate9-Mar-16 4:57 
Great stuff & truly honest with credits ! Bravo Wink | ;)
Questionwork 100% but i need help Pin
Mico Perez9-Jul-13 18:46
Mico Perez9-Jul-13 18:46 
AnswerRe: work 100% but i need help Pin
Jeremy Hutchinson10-Jul-13 4:48
professionalJeremy Hutchinson10-Jul-13 4:48 
GeneralRe: work 100% but i need help Pin
Mico Perez10-Jul-13 15:47
Mico Perez10-Jul-13 15:47 
GeneralRe: work 100% but i need help Pin
Mico Perez11-Jul-13 0:23
Mico Perez11-Jul-13 0:23 
QuestionAwesome! - Instead of 100% Screen Coverage Pin
stan_mcginnis26-Oct-12 5:39
stan_mcginnis26-Oct-12 5:39 
AnswerRe: Awesome! - Instead of 100% Screen Coverage Pin
Jeremy Hutchinson26-Oct-12 5:58
professionalJeremy Hutchinson26-Oct-12 5:58 
QuestionHow to make loading wait adorner visible while a window is created and showed? Pin
mary4wpf3-Oct-12 6:23
mary4wpf3-Oct-12 6:23 
AnswerRe: How to make loading wait adorner visible while a window is created and showed? Pin
Jeremy Hutchinson4-Oct-12 7:43
professionalJeremy Hutchinson4-Oct-12 7:43 
Suggestionabout the libraries Pin
Pinocs1-Jun-12 5:30
Pinocs1-Jun-12 5:30 
GeneralRe: about the libraries Pin
Jeremy Hutchinson1-Jun-12 5:34
professionalJeremy Hutchinson1-Jun-12 5:34 
QuestionIts not working with asynchronous call Pin
Member 34620802-Mar-12 20:22
Member 34620802-Mar-12 20:22 
AnswerRe: Its not working with asynchronous call Pin
Jeremy Hutchinson5-Mar-12 9:26
professionalJeremy Hutchinson5-Mar-12 9:26 
QuestionDispatcherPriority.ContextIdle cause unnormal! Pin
walker_ck18-Aug-11 22:36
walker_ck18-Aug-11 22:36 
GeneralLoadingWait and BackgroundWorker Pin
lmhf00428-Sep-10 15:38
lmhf00428-Sep-10 15:38 
GeneralRe: LoadingWait and BackgroundWorker Pin
Jeremy Hutchinson29-Sep-10 1:59
professionalJeremy Hutchinson29-Sep-10 1:59 
GeneralRe: LoadingWait and BackgroundWorker Pin
Member 173204230-May-11 7:25
Member 173204230-May-11 7:25 
GeneralUsing this with a tab control interface Pin
Timothy Lin Zheng Tian30-Jun-10 21:45
Timothy Lin Zheng Tian30-Jun-10 21:45 
GeneralRe: Using this with a tab control interface Pin
Jeremy Hutchinson1-Jul-10 2:30
professionalJeremy Hutchinson1-Jul-10 2:30 

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.