Coffeehouse Post

Single Post Permalink

View Thread: Why is WPF so complicated?
  • User profile image
    Ernie Booth

    This could be done the way your describing Cybermagellan even in WPF we could have something like this:


    <Border Background=”#ffffff” Padding=”12”>
    <Image x:Name=”image” Height=”200” Source=”normal.jpg” MouseLeftButtonDown=”OnPressed”/>


    And then in C# you would have


    void OnPressed (object sender, RoutedEventArgs e)
       this.image.Source = “pressed.jpg”

    But the reason that we choose to do it in Xaml is because it is much easier for a design tool to read xml then it is to read C# or VB.NET.  If you have done Windows Forms programming and edited the OnInitialized section you know how easy it is to break a code parser for a designer.  So WPF chooses to use a markup language so that designers can easily parse design files, this has already lead to some great third party design tools such as Aurora and Zam3D.  

    There is another more interesting reason to use Xaml which is that it works really great with the Model-View-Controller design pattern where the Xaml file is the view and the code beside file is the controller. As jmacdonagh points out this makes reuse much easier. 

    One thing to keep in mind is that Xaml is not what makes WPF so revolutionary it’s the flexibility that WPF allows via composition, data binding, styling, templating, resources and animation.

    ·         Composition: Element composition is at the core of WPF. Controls such as ListBoxes and Buttons are made up of simpler elements such as Borders, Shapes, Brush and Resources. A great example of composition in WPF is the Content property on most controls. Buttons for example don’t have a Text property, but instead of a Content property of type object. Any type of object can be assigned to this property, for example this Xaml snippet shows a Button being used as Content for another button. This results in the light blue button being placed inside of the green button.  Both buttons continue to work normally.


    <Button Background="Green">

    <Button Background="LightBlue">Hello, Composition</Button>




    Composition allows for very complex controls to be created without having to re-implement how they are render and hit tested.


    ·         Styling: UI development often requires setting the sample properties on different controls multiple times.  Styling is away to create the look of a control once and apply it to all the controls that should have that appearance.


    ·         Templating: Controls in WPF are look-less, meaning that their behavior is completely separate from their appearance. This allows the look of a control to be completely replaced without modifying its behavior or altering the class in which it is defined. There is no reason a button couldn’t be round, or use 3D animations for its state changes.  There is another type of templating called DataTemplating which allows for non user interface objects to be given a look based on their type. An example of this might be a Customer business object that could be added to a ListBox as an Item and have a DataTemplate control how a Customer is rendered.


    ·         Resources: are a reusability mechanism; they can be anything from an image to a Style.


    ·         Data Binding: is central to WPF in that just about any property in a control can be data bound to any CLR property.  Data Binding gets rid of event handlers which are used to simply update another property and contributes to a much cleaner implementation.  It also makes look-less controls in WPF possible as controls in a template are bound to properties in the behavior class.


    For a good technical overview check out this session from Mix06:

    For a full week of WPF check this out: