Click here to Skip to main content
Click here to Skip to main content
Go to top

Dynamic User Interfaces in WPF

, 8 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Reviews how to implement dynamic user interfaces in WPF.

Introduction

This article examines how to design and create dynamic user interfaces using the Windows Presentation Foundation (WPF). Along the way we will explore a demo application which uses XAML for creating a dynamic user interface to display rich and interactive alert messages to the user.

Background

The term "dynamic user interface" refers to a user interface which hosts arbitrary content discovered at runtime. Desktop applications traditionally consist of user interfaces which are designed and compiled up front, and then remain relatively static after the application is deployed. The static nature of a traditional desktop application imposes limitations on what type of content can be displayed. Many of those limitations have been more easily addressed with Web-based UI platforms, due to the fact that Web UIs are always downloaded and based on markup (namely, HTML).

With the advent of WPF it has become much easier to download and display portions of a user interface while the application is running. Not only can you easily display arbitrary UI content, but you can also associate runtime behavior with those visual elements. For example, it is fairly easy to create a WPF application which downloads a portion of its user interface and then reacts to, say, a dynamically loaded button being clicked by the user. This opens a wide range of new possibilities for desktop application developers.

Nothing is perfect

This article shows one way to create a dynamic user interface in WPF. There are certainly other approaches which you could use, such as downloading an assembly with a UserControl in it and then using reflection to load an instance of that control into your UI. The approach outlined in this article is XAML-based, which makes it very flexible and allows you to expose portions of a user interface via XML Web services.

Naturally each solution to a particular problem has its pros and cons, so there is no absolute "best" way to depend on. For example, the approach shown in this article expects the application to know how to respond to user interaction with elements in the dynamic UI content. If a user clicks on a dynamically loaded Hyperlink control, the application must respond accordingly. The approach involving a dynamically loaded UserControl would not have that restriction since the UserControl can contain its own interaction logic. However, since a UserControl cannot be downloaded as XML, you might encounter firewall/security issues if you choose to download portions of a dynamic UI as a DLL.

The three pillars

There are three essential steps to creating a dynamic user interface, if you choose to use the approach described in this article. We will review those three pillars in this section, and then see them in use in the next section.

XAML

The UI content which is dynamically loaded must be serialized in XAML. In other words, your application must be able to access some XAML and turn it into live visual elements which can be displayed. The secret ingredient in this process is the XamlReader.Load method.

Content Container

An application which hosts dynamic UI content must have a place to put that content. After you have turned some XAML into visual elements you need to display them somewhere. You can use a ContentControl or a ContentPresenter to contain the visual elements (read about the differences between those two elements here).

Interaction Notifications

Your dynamic UI should not be a trophy wife. If it does not do something other than look good, you probably don't need it around. You have two options for associating behavior with dynamically loaded visual elements: routed events and routed commands.

Since routed events/commands can bubble up the element tree you can add basic interaction logic in your application, at compile-time. When dynamic content is loaded and the user interacts with it, your pre-built interaction logic can perform the necessary actions in response to user input.

For example, if your dynamic UI will usually contain a Hyperlink in it, then your application should provide a means of responding to a Hyperlink being clicked. As I mentioned previously, the fact that the application must have baked-in logic to handle user interaction is a shortcoming of using the XAML-based approach to creating dynamic UIs. However, that shortcoming might not be an issue for applications with relatively simple dynamic UI needs.

What the demo application does

This article is accompanied by a demo application which shows how to implement a dynamic UI. The demo application is an "imaginary" business app (i.e. it has no actual functionality) which displays a dynamic alert message to the user.

The idea behind this use of dynamic user interfaces is that an alert message can be placed on a server, and when the application is run, it retrieves and displays the message. The alert is stored as XML, which happens to contain valid XAML (remember, XAML is an XML-based language). The alert XML data could be retrieved by the application via an XML Web service, or from a database call, or from a network drive, etc.

This dynamic alert message functionality allows the application to be deployed with no knowledge of what the alert messages might contain, or when they will exist. When an alert message is placed on a server, the application can simply download it and display whatever content it happens to contain. Since the alert is declared in XAML it can make use of the entire WPF platform; such as including images, video, audio, or even controls that do something in response to user input.

What the demo application looks like

Here is a screenshot of the alert message which is dynamically loaded when the demo application is run:

Screenshot of demo application

Everything in that alert interface is dynamically loaded, except for the gray title bar and the "[Close]" link at the bottom. If you were to click on the "here" link, it would open a Web browser to a specific page. The alert message uses the flow document technology in WPF to provide a smooth reading experience with a bulleted list. It would certainly be non-trivial to emulate this functionality in a Windows Forms application.

How the demo application works

Now that we have a firm understanding of what dynamic user interfaces are and the fundamental steps required to implement them in WPF, let's take a look how the demo application works. There are five pieces in this puzzle.

Alert XML data

First we will take a look at how an alert is declared. An alert is saved as XML, which can be retrieved from a server at runtime. Here is the alert XML data used in the demo application:

<?xml version="1.0" encoding="utf-8" ?>
<Alert Title="Company News">
  <!--<span class="code-comment"> The inner XML of the <Alert> element is valid XAML. --></span>
  <FlowDocumentScrollViewer 
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    VerticalScrollBarVisibility='Auto'
    >
    <FlowDocument>
      <Paragraph>
        The Foo Daddy Company is proud to announce
        that it has a new Vice President, Ronald McDonald.
        Mr. McDonald joins us after a long tenure at the
        McDonald's Corporation, serving as Chief Happiness
        Officer for over 20 years.  Ronald McDonald can
        speak 31 different languages including:
      </Paragraph>
      <List>
        <ListItem>
          <Paragraph>Mandarin</Paragraph>
        </ListItem>
        <ListItem>
          <Paragraph>Dutch</Paragraph>
        </ListItem>
        <ListItem>
          <Paragraph>Tagalog</Paragraph>
        </ListItem>
        <ListItem>
          <Paragraph>Hindi</Paragraph>
        </ListItem>
      </List>
      <Paragraph>
        Read more about Mr. McDonald's background
        <Hyperlink 
          NavigateUri='http://en.wikipedia.org/wiki/Ronald_Mcdonald' 
          ToolTip="View Mr. McDonald's profile">
          here
        </Hyperlink>.
      </Paragraph>
    </FlowDocument>
  </FlowDocumentScrollViewer>
</Alert>

There are two important aspects in the XML data seen above. The root <Alert> element has a Title attribute. The title is displayed above the alert content in the user interface. The inner XML of the <Alert> element is the XAML which constitutes the alert's message. That XAML is dynamically loaded and rendered by the host application.

The Alert class

The title and content of an alert need to be stored somewhere. I chose to store that information in a simple class called Alert. Here is the entire Alert class:

/// <span class="code-SummaryComment"><summary></span>
/// Stores information about an alert.
/// <span class="code-SummaryComment"></summary></span>
public class Alert
{
 readonly object content;
 readonly string title;

 public Alert( string title, object content )
 {
  if( content == null )
   throw new ArgumentNullException( "content" );

  // If the alert's title was not specified use a default value.
  this.title = String.IsNullOrEmpty(title) ? "Alert" : title;

  this.content = content;
 }

 public object Content
 {
  get { return this.content; }
 }

 public string Title
 {
  get { return this.title; }
 }
}

Main application Window

In this demo application the main Window does not really do much. It has no real functionality. All that it does is check to see if an alert exists, and then displays it if one does exist. In a real application the main Window would certainly have more to it than this, but let's keep it simple for the sake of this demo. Here's the code-behind for the main Window:

/// <span class="code-SummaryComment"><summary></span>
/// This is the main Window of an imaginary business app.
/// <span class="code-SummaryComment"></summary></span>
public partial class MainWindow : Window
{
 public MainWindow()
 {
  InitializeComponent();

  // When the Window loads, check to see if 
  // there is an alert to display.
  this.Loaded += delegate
  {
   this.CheckForAvailableAlert(); 
  };  
 }

 /// <span class="code-SummaryComment"><summary></span>
 /// Checks to see if an alert needs to be shown.
 /// If so, it shows the alert message in a Window.
 /// <span class="code-SummaryComment"></summary></span>
 void CheckForAvailableAlert()
 {
  Alert alert = AlertProvider.GetLatestAlert();
  if( alert != null )
   new AlertWindow( this, alert ).ShowDialog(); 
 }
}

AlertProvider

The main Window relies on a class called AlertProvider to determine if there is an alert to display to the user. AlertProvider is responsible for "checking" if there is an alert to display and, if there is, returning information about it. Keep in mind that in this demo the actual alert data is not being retrieved from an external source. The alert XML is a resource in the application assembly, but in a real application the alert would be retrieved from an external source. Here is the sole public member of AlertProvider:

/// <span class="code-SummaryComment"><summary></span>
/// This method retrieves an alert to display.  
/// If no alert is available it returns null.
/// <span class="code-SummaryComment"></summary></span>
public static Alert GetLatestAlert()
{
 Alert alert = null;
 XmlTextReader xmlRdr = null;
 try
 {
  xmlRdr = RetrieveAlertXml();
  bool alertExists = xmlRdr != null;
  if( alertExists )
   alert = CreateAlertFromXml( xmlRdr );
 }
 finally
 {
  if( xmlRdr != null )
   xmlRdr.Close();
 }
 return alert;
}

That method depends on two private helper methods; RetrieveAlertXml and CreateAlertFromXml. The former is implemented like so:

/// <span class="code-SummaryComment"><summary></span>
/// In this demo we keep it simple and just load some XML
/// out of a resource in this assembly.  In a real app you
/// might want to access a Web service in this method, to 
/// get the latest XML alert message, if one exists.
/// <span class="code-SummaryComment"></summary></span>
static XmlTextReader RetrieveAlertXml()
{
 try
 {
  Uri uri = new Uri( "AlertData.xml", UriKind.Relative );
  StreamResourceInfo info = Application.GetResourceStream( uri );
  return new XmlTextReader( info.Stream );
 }
 catch( Exception ex )
 {
  Debug.WriteLine( "Did not get Alert XML: " + ex );
  return null;
 }
}

The method which converts the retrieved XML into an Alert object is seen below:

/// <span class="code-SummaryComment"><summary></span>
/// Creates an Alert object based on the specified XML.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="xmlRdr">Contains data about an alert.</param></span>
static Alert CreateAlertFromXml( XmlTextReader xmlRdr )
{
 // Load the XML data into an XmlDocument.
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load( xmlRdr );

 // Get the root <Alert> element.
 XmlElement alertElem = xmlDoc.DocumentElement;

 // Grab the alert's title off the <Alert> element.
 string title = alertElem.GetAttribute( "Title" );

 // Create visual objects out of the <Alert>
 // element's inner XML (which must be valid XAML).
 object content = DeserializeXaml( alertElem.InnerXml );

 // Bundle up the information we retrieved and return it.
 return new Alert( title, content );
}

The real magic behind converting a string full of XAML into visual elements is implemented like this:

/// <span class="code-SummaryComment"><summary></span>
/// Instantiates and returns the object(s) declared 
/// in the specified XAML.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="xaml">Valid XAML markup text.</param></span>
static object DeserializeXaml( string xaml )
{
 using( MemoryStream stream = new MemoryStream() )
 {
  // Convert the text into a byte array so that 
  // it can be loaded into the memory stream.
  byte[] bytes = Encoding.UTF8.GetBytes( xaml );

  // Write the XAML bytes into a memory stream.
  stream.Write( bytes, 0, bytes.Length );

  // Reset the stream's current position back 
  // to the beginning so that when it is read 
  // from, the read begins at the correct place.
  stream.Position = 0;

  // Convert the XAML into a .NET object.
  return XamlReader.Load( stream );
 }
}

AlertWindow

Last but not least we have the Window which serves as a dynamic content container and a source of interaction logic. The AlertWindow class is used as a modal dialog to display the dynamically loaded UI content. Here is a portion of the XAML declaration for AlertWindow:

<!--<span class="code-comment"> Add a handler to the RequestNavigate event of Hyperlink
     so that if a dynamically loaded Hyperlink is clicked, 
     we can open the Web page to which it points. --></span>
<Grid Hyperlink.RequestNavigate="OnHyperlinkRequestNavigate">
  <!--<span class="code-comment"> Associate an event handling method with the Executed 
       event of the Close command. --></span>
  <Grid.CommandBindings>
    <CommandBinding 
      Command="{x:Static ApplicationCommands.Close}" 
      Executed="OnCloseCommandExecuted" 
      />
  </Grid.CommandBindings>

  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>

  <!--<span class="code-comment"> ALERT TITLE HEADER --></span>
  <TextBlock Grid.Row="0"
    Background="Gray" 
    FontSize="23" 
    Foreground="White"         
    Text="{Binding Path=Title}" 
    TextAlignment="Center" 
    />

  <!--<span class="code-comment"> ALERT CONTENT CONTAINER --></span>
  <ContentControl Grid.Row="1"
    Content="{Binding Path=Content}"          
    />

  <!--<span class="code-comment"> [CLOSE] HYPERLINK --></span>
  <TextBlock Grid.Row="2" 
    FontSize="15"        
    HorizontalAlignment="Center" 
    Margin="6"
    >
    [<Hyperlink Command="Close">Close</Hyperlink>]
  </TextBlock>
</Grid>

As you can see above, the Grid panel which contains all of the other elements has two interesting settings applied to it. The attached event syntax is used to add a handler for the Hyperlink class's RequestNaviate bubbling routed event. This ensures that when the user clicks on a dynamically loaded Hyperlink the OnHyperlinkRequestNavigate method in the code-behind will be invoked. The Grid also has a CommandBinding established for the Close command. That binding ensures that the AlertWindow is notified when the user wants to close it.

Here is the code-behind for AlertWindow:

/// <span class="code-SummaryComment"><summary></span>
/// Displays an alert message.
/// <span class="code-SummaryComment"></summary></span>
public partial class AlertWindow : System.Windows.Window
{
 public AlertWindow( Window owner, Alert alert )
 {
  InitializeComponent();

  this.Owner = owner;
  this.DataContext = alert;   
 }

 void OnCloseCommandExecuted( object sender, ExecutedRoutedEventArgs e )
 {
  this.Close();
 }

 void OnHyperlinkRequestNavigate( object sender, RequestNavigateEventArgs e )
 { 
  Process.Start( e.Uri.AbsoluteUri );
 }
}

Conclusion

This article demonstrates the exciting potential of dynamic user interfaces in WPF applications. Through the use of XAML, a content container, and routed interaction notifications we were able to make use of arbitrary UI content at runtime. It is also important to note that the technique described in this article is not the only approach you can use to create dynamic UIs. Before implementing a dynamic UI in your application, be sure to consider other options to make sure that the most appropriate technique is used.

License

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

Share

About the Author

Josh Smith
Software Developer (Senior) Cynergy Systems
United States United States
Josh creates software, for iOS and Windows.
 
He works at Cynergy Systems as a Senior Experience Developer.
 
Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.
 
Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.
 
Check out his Advanced MVVM[^] book.
 
Visit his WPF blog[^] or stop by his iOS blog[^].
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberChampion Chen28-Apr-14 19:29 
QuestionUnknown "StackPanel" Error PinmemberVikasumit11-Sep-13 21:58 
AnswerRe: Unknown "StackPanel" Error PinmemberVikasumit11-Sep-13 22:17 
GeneralMy vote of 5 Pinmemberumarinam13-Aug-10 6:49 
QuestionRealy nice ! but how to in MvvM ??? PinmemberPatrick Antonioli18-Jun-10 2:35 
GeneralMessage Automatically Removed Pinmembersteveusa3920007-Sep-07 13:57 
QuestionUserControls? Pinmemberadamsd529-Jul-07 20:53 
GeneralSome Comments [modified] PinprotectorMarc Clifton10-Jun-07 6:46 
GeneralRe: Some Comments PinmvpJosh Smith10-Jun-07 7:54 
GeneralRe: Some Comments PinprotectorMarc Clifton10-Jun-07 8:28 
GeneralRe: Some Comments PinmvpJosh Smith10-Jun-07 8:41 
GeneralRe: Some Comments PinprotectorMarc Clifton10-Jun-07 8:56 
GeneralRe: Some Comments PinmemberAlexY10-Jun-07 16:40 

Marc Clifton wrote:
Josh Smith wrote:
Demo apps should not be reference apps. They need to follow the KISS principle, otherwise the signal gets lost in the noise.
 

And, in my experience, the general programming community doesn't learn much past what they see in demo apps. Which came first? I tend to believe that demo apps are dumbing down programmers.

 
Actually, I am with Josh on this one. Good programmers tend to take a concept and put to a good use by extending it for their needs. As for 'not good' ones have an not trivial demo would not help anyway...

GeneralRe: Some Comments Pinmemberjrdnln11-Jun-07 4:10 
GeneralRe: Some Comments PinprotectorMarc Clifton11-Jun-07 14:52 
GeneralRe: Some Comments PinmvpJosh Smith11-Jun-07 15:18 
GeneralRe: Some Comments PinmemberThesisus26-Sep-12 2:59 
GeneralRe: Some Comments PinmemberScott Dorman24-Sep-07 5:06 
GeneralRe: Some Comments PinmemberScott Dorman24-Sep-07 5:02 
GeneralRe: Some Comments PinmvpJosh Smith10-Jun-07 17:53 
GeneralRe: Some Comments PinmemberWillemM11-Jun-07 1:59 
GeneralRe: Some Comments PinmvpJosh Smith11-Jun-07 2:27 
GeneralRe: Some Comments PinprotectorMarc Clifton11-Jun-07 14:56 
GeneralRe: Some Comments PinmvpJosh Smith11-Jun-07 15:10 
QuestionAcropolis? Pinmembersdahlbac9-Jun-07 13:58 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 8 Jun 2007
Article Copyright 2007 by Josh Smith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid