Lovin’ WPF

Recently we’ve been moving more and more towards using WPF to deliver LOB applications (Line Of Business). It took me a while to persuade the team that WPF was something worth investing time and effort in, which is funny really because they normally jump at the chance to play with new technologies. So, how did I do it? Ironically – it was with a hugely trivial example; a status bar. Basically I showed them how the same code could be written in markup in two ways. The first one was:

<Window x:Class="WorkforcePF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="300" Width="300">
  <DockPanel>
    <StatusBar Name="statusBar1" Height="20" VerticalAlignment="Bottom"
      DockPanel.Dock="Bottom">
      <StatusBar.ItemsPanel>
        <ItemsPanelTemplate>
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="4*" />
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition/>
            </Grid.ColumnDefinitions>
          </Grid>
        </ItemsPanelTemplate>
      </StatusBar.ItemsPanel>
      <StatusBarItem>Ready</StatusBarItem>
      <StatusBarItem Grid.Column="1" Content="StatusBarItem" />
    </StatusBar>
  </DockPanel>
</Window>

while the second one was:

<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="StatusBarInWpf.Window1"
 x:Name="Window"
 Title="Window1"
 Width="640" Height="480">

 <Window.Resources>
  <ItemsPanelTemplate x:Key="StatusBarItemTemplate">
   <Grid>
    <Grid.ColumnDefinitions>
     <ColumnDefinition Width="4*"/>
     <ColumnDefinition Width="Auto"/>
     <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
     <RowDefinition/>
    </Grid.RowDefinitions>
   </Grid>
  </ItemsPanelTemplate>
 </Window.Resources>

 <DockPanel>
  <StatusBar Height="20" DockPanel.Dock="Bottom" ItemsPanel="{DynamicResource StatusBarItemTemplate}">
   <StatusBarItem Content="Ready" Grid.Column="0"/>
   <StatusBarItem Content="StatusBarItem" Grid.Column="1"/>
  </StatusBar>
 </DockPanel>
</Window>

The thing I love about the second version is the way that the StatusBar template is removed from the actual implementation of the StatusBar, so you can move them into one place (currently in the Window.Resources but this could be put into a named template or the Application.Resources) and manage them really easily.

Advertisements

6 thoughts on “Lovin’ WPF

  1. Pingback: Still lovin’ WPF « Musings and frustrations

  2. Pingback: WPF/Silverlight/XAML Web News - 2008/04/25 - Rob Relyea - Xamlified

  3. Are there any specific advantages that you found when using WPF? I am starting a new application, and would love to hear about your experience. Does it work well on Windows XP, Windows 2003?

  4. peteohanlon

    Thomas – We’ve found some real advantages in using WPF. Possibly the biggest advantage for us is the binding support – by using a trigger on an element for instance, we can change the colour of another item to indicate that it is outside a particular threshold (all done in the XAML).

  5. peteohanlon

    Evan – I know what you mean. The more you do with WPF, the more you find you can do. It’s the flexibility and the power that is so appealing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s