BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - Decorator

The thirty-fourth part of the Windows Presentation Foundation Fundamentals tutorial describes the Decorator class. This is another base class for WPF controls, including two of the layout controls seen earlier in the tutorial.

Decorator

Continuing the examination of the base classes for the WPF layout controls that we've seen in earlier instalments of the WPF tutorial brings us to the Decorator class. This is another relatively simple base class. Decorators have a single child control, to which they apply some effect. We have already seen two controls that derive from Decorator. Border adds a rectangular border, with optional rounded corners, around its child control. ViewBox changes the size of its child, stretching it horizontally and vertically to fill a given space. In both cases the child control need not know about the effect of its parent.

To demonstrate the use of decorators, create a new WPF application project in Visual Studio, naming the project, ";DecoratorDemo";. Once loaded, replace the XAML of the main window with that shown below. This creates a Border containing a Button control. It shows how the child control of a Decorator is set; it is described between the opening and closing tags of its parent's XAML element.

<Window x:Class="DecoratorDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Decorator Demo"
        Width="250"
        Height="200">
    <Border Name="MyBorder" BorderBrush="Black" BorderThickness="4" CornerRadius="10">
        <Button>Hello, world!</Button>
    </Border>
</Window>

When the program is executed the resultant window appears similar to the image below:

WPF Decorator Class

Child Property

In the first example we set the content of the Border using a control defined within its XAML element. You can also set the child control using C# code. To do so you set the Child property. Unlike other base classes that we've seen, the Child property can only be set to an object of a type that inherits from UIElement. This does include all WPF controls but nono of other types that you might use with a ContentControl or HeaderedContentControl.

To demonstrate let's add a Click event to the Button. Replace the Button's XAML with the following:

<Button Click="Button_Click">Hello, world!</Button>

In the code behind the window add the method that will be called when the button is clicked. The method below replaces the Button with a new Label.

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyBorder.Child = new Label
    {
        Content = "Goodbye, cruel world!",
        HorizontalAlignment = HorizontalAlignment.Center,
        VerticalAlignment = VerticalAlignment.Center
    };
}

Try running the program and clicking the button to see the results.

WPF Decorator Class Child Property

26 October 2013