Running on Empty

The few things I know, I like to share.

WPF Popup control. (Part 1, the Quick and Dirty Way)

Introduction

One of my favorite features in WPF is the separation of functionality and display of visuals in the UI.  So I thought I would start a new series demonstrating some cool features I have used in WPF, starting with Popup controls.

Declaring a Popup control, the Quick and Dirty way.

The quickest and easiest way to create a pop up control is to declare the Popup in XAML.  I chose to use a Viewbox in this example to make sizing a little easier.  The following code can be added to any Panel or Canvas.

<Popup x:Name="PopupTest" AllowsTransparency="True">
    <Viewbox VerticalAlignment="Top">
        <TextBlock Text="Wow, that was easy!"/>
    </Viewbox>
</Popup>

Showing the Popup control.

Now create an event handler based on an action in your window.  I chose the MouseEnter event in this sample.The main parameters that need to be set here are Placement, StaysOpen and IsOpen.  Placement sets where the Popup will actually be placed, PlacementMode.Mouse is most likely what you want.  StaysOpen determines if the Popup will close if it loses focus.  Finally, IsOpen will make the Popup visible.

void Window_MouseEnter(object sender, MouseEventArgs e)
{
    PopupTest.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
    PopupTest.StaysOpen = false;
    PopupTest.Height = 1000;
    PopupTest.Width = 500;
    PopupTest.IsOpen = true;
}

Conclusion

Like I said, quick and dirty.  This method of Popups probably should be used any time you want a Popup that doesn’t need to be replicated throughout your application and doesn’t require dependency properties.  In future articles of this series I will demonstrate using some more complex Popup controls. 

I look forward to your comments and suggestions.  Please feel free to request future articles.

Advertisements

January 7, 2008 - Posted by | C#, WPF, XAML

8 Comments »

  1. I just read your article and got excited that you were going to write about more complex Popup controls in a future article, then I saw that the date was 3 days ago…. I anxiously await your future article.

    Comment by Jeremy Chaney | January 10, 2008 | Reply

  2. Hmm, did you check out Part 2 of this series? I would be happy to demonstrate more complex popup examples. Perhaps, you have something you would like to see?

    Comment by roecode | January 10, 2008 | Reply

  3. Your plugin that shows code and allows viewers to copy it to the clipboard is fabulous. Unfortunately it adds the occasional   but otherwise it’s great. Where did you find it?

    Comment by Rob Burke | January 14, 2008 | Reply

  4. I use the [sourcecode] tags, it is a built in “feature” of WordPress. I dont always like what it does to the code though.

    http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/

    Comment by roecode | January 14, 2008 | Reply

  5. Thank you for the example code. It was short sweet, and just what I was looking for!

    Comment by Mel Green | July 15, 2009 | Reply

  6. Is there way to make the wpf popup Modal at all? I noticed that the popup control doesn’t lock down the application.

    Comment by Cory | September 1, 2009 | Reply

    • Are you sure you are looking for a popup? I think you might be better off with a modal window for that case.

      Comment by roecode | September 1, 2009 | Reply

  7. […] = false; PopupTest.Height = 1000; PopupTest.Width = 500; PopupTest.IsOpen = true; } source: roecode keywords: […]

    Pingback by Create Popup in WPF and C# « Angga's Pc is Back! | May 26, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: