Channels ▼


What's New In WPF 4.5

The Clipboard ribbon group (RibbonGroup), which is named ClipboardGroup, is similar than the classic Clipboard group provided in Microsoft Word. There is a Paste ribbon menu button (RibbonMenuButton) that displays two ribbon menu items when the user clicks or presses the accelerator key (see Figure 11).

Figure 11: The Paste ribbon menu button displaying the two ribbon menu items.

In addition, the Clipboard ribbon group displays three ribbon buttons (RibbonButton): Cut, Copy, and Format Painter. You will notice that the XAML code specifies the image for the Paste ribbon menu button in the LargeImageSource property instead of the SmallImageSource property used for the three ribbon buttons. Thus, the icon for Paste is larger than the icons for Cut, Copy, and Paste. The idea was to reproduce the same kind of layout provided by Microsoft Word, but with the new native Ribbon control included in WPF 4.5.

The Colors ribbon group (RibbonGroup), named Color, displays three ribbon radio buttons (RibbonRadioButton). As you might guess, the behavior of the RibbonRadioButton is similar to the well-known radio button and only one of them that can be checked. In the example, the Red ribbon radio button has the IsChecked property set to True and appears as the selected option (as in Figure 10).

The following lines define the Launch tab with two ribbon groups: Desktop Applications and Apps. In this case, each ribbon group is empty to keep the example simple.

<RibbonTab Header="Launch" KeyTip="L">
    <!-- Launch/Applications group-->
    <RibbonGroup x:Name="DesktopApplication" Header="Desktop Applications">
    <!-- Launch/Games group-->
    <RibbonGroup x:Name="App" Header="Apps">

If you want to preview the Launch tab in the XAML preview, you just need to change the value for the SelectedIndex property of the Ribbon control to 1:

<Ribbon x:Name="Ribbon" SelectedIndex="1">

This way, you are able to preview how the different controls for each tab look within the Ribbon without having to execute the application (as you do with other WPF controls); see Figure 12.

Figure 12: The Launch tab with the two empty ribbon groups displayed in the XAML preview.

The Ribbon control has been around for a while, but now WPF 4.5 includes the control as a native library, so I wanted to provide a detailed example of many of the sub-controls it provides. Of course, you can use everything you know about bindings, commands, and Model-View-ViewMode (MVVMl) to fire actions when the user interacts with the different Ribbon sub-controls.

New Methods for Dispatcher

Whenever you need to perform asynchronous operations with WPF, your code makes extensive use of the methods provided by the Dispatcher class ( System.Windows.Threading.Dispatcher) to schedule updates to the UI controls from different threads. The Dispatcher class in WPF 4.5 has new methods to simplify some common tasks and to provide compatibility with asynchronous methods introduced in .NET Framework 4.5. In Using Asynchronous Methods in ASP.NET 4.5 and in MVC 4, I provided some examples of the asynchronous methods with WPF 4.5 and their naming conventions.

The five new definitions of the InvokeAsync method are obviously asynchronous (they use the Async suffix) and they allow you to use the C# 5.0 await keyword to wait for the asynchronous execution to be completed. You can use InvokeAsync to execution either a specified Action or a Func<TResult> asynchronously, and you can also specify the desired DispatcherPriority and a CancellationToken if you want the execution to support cancelation.

The following list provides all the definitions for the new InvokeAsync method:

  • InvokeAsync(Action)
  • InvokeAsync(Action, DispatcherPriority)
  • InvokeAsync(Action, DispatcherPriority, CancellationToken)
  • InvokeAsync<TResult>(Func<TResult>)
  • InvokeAsync<TResult>(Func<TResult>, DispatcherPriority)
  • InvokeAsync<TResult>(Func<TResult>, DispatcherPriority, CancellationToken)

The following lines show a simple example of an asynchronous method that invokes an asynchronous action that changes the Content property for UIElement. The ChangeUIElementContent will return when the action passed as a parameter to InvokeAsync completes its execution. You can use the other method definitions to specify a value to DispatcherPriority and CancellationToken.

private async void ChangeUIElementContent()
    await Dispatcher.InvokeAsync(() => UIElement.Content = "Change my content");

The following lines show another simple example of an asynchronous method that invokes an asynchronous Func<string> that changes the Content property for UIElement and returns the new Content value. The ChangeUIElementContent will return the new Content value when the Func<string> passed as a parameter to InvokeAsync completes its execution.

private async string ChangeUIElementContent()
    var newContent = await Dispatcher.InvokeAsync(
        () =>
                UIElement.Content = "Change my content";
                return UIElement.Content;
    Return newContent;

If you worked with previous WPF versions, you will notice that it is easy to return a value because InvokeAsync supports Func<TResult> as a parameter. The good news is that there is also a synchronous version of the Invoke method that allows you to easily return a value without workarounds. The following list provides all the definitions for the new synchronous Invoke<TResult> method that receive a Func<TResult> as the main parameter:

  • Invoke<TResult>(Func<TResult>)
  • Invoke<TResult>(Func<TResult>, DispatcherPriority)
  • Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken)
  • Invoke<TResult>(Func<TResult>, DispatcherPriority, CancellationToken, TimeSpan)

As shown in the aforementioned list, the new synchronous versions of the Invoke method also allow you to specify a value to DispatcherPriority and CancellationToken. In addition, you can set the desired TimeSpan.

You will definitely enjoy using the new Dispatcher methods that allow you to return values without workarounds because the code is easier to read. The new asynchronous methods are essential when targeting different environments, such as WPF, ASP.NET MVC 4, and Windows 8 apps.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.