... a journey through WPF, MVVM and .NET4 RSS 2.0
# Thursday, April 03, 2008
A colleague at DevelopMentor recently asked me about creating popup windows in XAML browser applications (XBAP). Normally this is not allowed - if you try to create a top-level window you will get a SecurityException because WPF asks for UIPermission which is strictly prohibited when hosted in the browser. It turns out, however, that you can get a popup window - there's a hidden little gem in the System.Windows.Controls.Primitive namespace that is your friend: Popup. It's the same underlying class that ToolTip, Menu, and ComboBox use to display drop-down menus and overlays and it is browser-hosting aware! It's pretty limited in functionality - I'm not sure you can get it to move around with the mouse for example, but for simple cases it works great. Here's a code snippet - wire this up to a button in an XBAP:
 
   1: void OnClick(object sender, EventArgs e)
   2: {
   3:    Popup window = new Popup();
   4:   
   5:    StackPanel sp = new StackPanel { Margin = new Thickness(5) };
   6:    sp.Children.Add(new TextBlock { Text = "Hi from a popup" });
   7:    Button newButton = new Button { Content = "Another button" };
   8:    newButton.Click += delegate { window.IsOpen = false; };
   9:    sp.Children.Add(newButton);
  10:    sp.Children.Add(new Slider { Minimum = 0, Maximum = 50, Value = 25, Width = 100 });
  11:   
  12:    window.Child = new Border { Background = Brushes.White, BorderBrush = Brushes.Black, 
BorderThickness = new Thickness(2), Child = sp };
  13:    window.PlacementTarget = this;
  14:    window.Placement = PlacementMode.Center;
  15:   
  16:    window.IsOpen = true;
  17: }

The key thing you need to do is set the PlacementTarget. That associates a "parent" window and without it, the Popup class asserts UIPermission which will fail in the browser environment.
Thursday, April 03, 2008 9:47:05 AM (Central Daylight Time, UTC-05:00)  #    Comments [0] -
.NET | WPF
I'm a WPF Disciple
Search
Categories
Archive
<April 2008>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Mark Smith
Sign In
All Content © 2010, Mark Smith
DasBlog theme 'Business' created by Christoph De Baene (delarou)