Ioannis Panagopoulos blog

Tutorials on HTML5, Javascript, WinRT and .NET

Porting a WPF application to Windows8

by Ioannis Panagopoulos

(for a brief intro in Windows8 visit first this post)

In this post, we will take a simple application implemented in WPF for Windows (Vista/XP SP3) and try to change it in a Windows8 “Metro” style application. During this process, we will evaluate two things:


  • If the application runs as is (the executable) in Windows8 when it is compiled in Visual Studio 2010 in Windows 7.
  • If we want to change the application for the new “Metro” style will it compile directly or will we need to do some changes and which and how big are those?


This demo is performed on a virtual machine running the Windows8 Developer preview presented in this post. Therefore keep in mind that we may be drawing some conclusions based on a “preview” product that will not reflect the complete reality. Also the implementation for Windows8 will be performed with the Visual Studio 2011 Developer Preview that runs from within Windows8. This edition allows only a particular kind of projects to be implemented called “Metro” style which are the ones for the new Windows8 UI. This does not mean that Visual Studio 2011 will not support “WPF Applications”. If you install the Visual Studio 2011 Preview for Windows7 all your loved project templates are there. In this post though, we want to see if our current knowledge can be used to implement the “new style” applications and therefore we target the new templates.

A screenshot of the application we are going to implement is shown below:




We have a list of clients displayed in a Listbox at the top of the window. Whenever a name is selected, its details are displayed at the bottom and can be edited. If we click on the button “Validate”, the client data are validated and a message is displayed showing the result of the validation process. Our entity in this application is represented by a class named Client:


public class Client : INotifyPropertyChanged {
   private int _id;
   public int Id {
      get { return _id; }
      set { _id = value; 
            if (PropertyChanged != null) { 
                PropertyChanged(this, new PropertyChangedEventArgs("Id")); 
                PropertyChanged(this, new PropertyChangedEventArgs("Description")); } }
   private string _name;
   public string Name {
      get { return _name; }
      set { _name = value; 
            if (PropertyChanged != null) { 
                PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
                PropertyChanged(this, new PropertyChangedEventArgs("Description")); } }
   public string Description {
      get { return this.ToString(); }
   public override string ToString() {
      return String.Format("{0,5} {1,-40}", Id, Name);
   public event PropertyChangedEventHandler PropertyChanged;

That is, our class implements the INotifyPropertyChanged interface and also raises the notification for the “Description” property whenever the Id or the Name properties are changed in order for the Listbox contents (databound to Description) to be refreshed. Our ViewModel for the application is as follows:


public class MainWindowViewModel : INotifyPropertyChanged
   private List<Client> _clients;
   public List<Client> Clients
      get { return _clients; }
      set { _clients = value; 
         if (PropertyChanged != null) 
             PropertyChanged(this, new PropertyChangedEventArgs("Clients")); }
    private Client _selectedClient;
    public Client SelectedClient
       get { return _selectedClient; }
       set { _selectedClient = value; 
          if (PropertyChanged != null) 
              PropertyChanged(this, new PropertyChangedEventArgs("SelectedClient")); }
     public MainWindowViewModel() {}
     public void Validate() {
        if (SelectedClient != null) {
           if (String.IsNullOrWhiteSpace(SelectedClient.Name))
              MessageBox.Show("Name cannot be empty for client");
              MessageBox.Show("All ok");
     public event PropertyChangedEventHandler PropertyChanged;


Again, the ViewModel implementes the INotifyPropertyChanged interface and has the Clients property which will be databound to the ItemSource of the Listbox and the SelectedClient property which will be databound to the SelectedItem of the Listbox and to the detail Textboxes at the bottom of the window. Moreover, it provides the Validate method for validating the results. The XAML file describing the UI of the window is as follows:


<Window x:Class="AskADev.Article1.MainWindow"
        Title="Demo WPF porting to Windows8" Height="400" Width="544" Loaded="Window_Loaded">
        <Canvas DockPanel.Dock="Bottom" Height="100" Background="Beige">
            <TextBlock Text="Id" Canvas.Left="6" Canvas.Top="12" />
            <TextBox Text="{Binding SelectedClient.Id}" Canvas.Left="79" 
                                                        Canvas.Top="9" Width="72" />
            <TextBlock Text="Name" Canvas.Left="6" Canvas.Top="40" />
            <TextBox Text="{Binding SelectedClient.Name}" Canvas.Left="79" 
                                                          Canvas.Top="37" Width="311" />
            <Button x:Name="ButtonSubmit" Height="31" Width="73" Canvas.Left="443" 
                   Canvas.Top="63" Click="ButtonSubmit_Click">Validate</Button>
        <ListBox DockPanel.Dock="Top" ItemsSource="{Binding Clients}" 
                 SelectedItem="{Binding SelectedClient}" DisplayMemberPath="Description"/>


Where you can see the data bound properties. Finally the XAML.cs file initializes the ViewModel, attaches it to the DataContext and also provides the event handler for the button:


private void Window_Loaded(object sender, RoutedEventArgs e)
    ViewModel = new MainWindowViewModel();
    DataContext = ViewModel;
private void ButtonSubmit_Click(object sender, RoutedEventArgs e)

We compile this and run it on a Windows7 machine and it runs correctly. Now we start the Windows8 Virtual Machine (instructions on how to install Windows8 on your desktop can be found in this post), get the executable we have created and run it on Windows8 “as is”:


Obviously, as expected, the application runs without any problems. But it runs on “Desktop” mode, while we want to make it run on the nice new Metro-Style UI. We open Visual Studio 2011 from within Window8 and select New Project/Templates/Visual C#/Windows Metro Style/Application:


The windows in this project template are more “Silverlight” like having as their root content a UserControl and not a Window. Therefore we leave the root declaration intact and copy/paste the inner contents of the original XAML.cs file. The changes we need to make are as follows (all changes are characterized as major/moderate/minor based on the time it will take you to make the changes in a full blown WPF app):


  • 1 (moderate) The Window must be converted to UserControl.
  • 2 (major) The “Dockpanel” is not supported (which is kinda expected since we will have a fixed size full screen application) and therefore we change it to a “Canvas” layout.
  • 3 (major) The default mode for the binding in now “One Way”. Therefore we need to specify the binding explicitly to “TwoWay”.
  • 4 (major) Textboxes get enlarged in the new “Metro” style (since they are intended for touch UIs) therefore we need to increase the spacing between the Textboxes (add approximately 10pixels). Of course if we had initially used a Stackpanel for the placement of our controls this would not be needed (good design always handles change more easily Smile)
  • 5 (minor) We need to change the colors to reflect the new style (I chose darker shades)


The new XAML is as follows (changes in yellow):


<UserControl x:Class="AskADev.Article1.Windows8.MainPage"
    mc:Ignorable="d" Loaded="Window_Loaded"
    d:DesignHeight="768" d:DesignWidth="1366">

        <Canvas Canvas.Top="668" Width="1366" Height="100">
            <TextBlock Text="Id" Canvas.Left="6" Canvas.Top="12" />
            <TextBox Text="{Binding SelectedClient.Id,Mode=TwoWay}" 
                     Canvas.Left="79" Canvas.Top="9" Width="72" />
            <TextBlock Text="Name" Canvas.Left="6" Canvas.Top="50" />
            <TextBox Text="{Binding SelectedClient.Name,Mode=TwoWay}" 
                     Canvas.Left="79" Canvas.Top="47" Width="311" />
            <Button x:Name="ButtonSubmit" Height="31" Width="73" Canvas.Left="443" 
                    Canvas.Top="63" Click="ButtonSubmit_Click">Validate</Button>
        <ListBox Background="Black" Foreground="White" Width="1366" Height="668" 
                 ItemsSource="{Binding Clients}" 
                 SelectedItem="{Binding SelectedClient,Mode=TwoWay}" 

The XAML.CS file (apart from some minor changes in the name) remains the same.

Finally, the ViewModel’s file is added as is to the application. The issues that we encounter here are the following:


  • 6 (major) The MessageBox is not supported. Therefore you will need to find another way to inform your users about the result (probably from within a special place of the UI). In this app we will just comment it out and do nothing. In real life we would have to change the UI to have a “Messages” area.
  • 7 (minor – but annoying until you discover it) Your application will compile if you leave the INotifyPropertyChanged interface to be defined in the System.ComponentModel assembly as it is for WPF. But the binding will not work! You need to replace the “using System.ComponentModel” statement with “using Windows.Ui.Xaml.Data” which also defines the INotifyPropertyChanged interface and the bindings will magically work!


We recompile and run the up and we are in “Metro”!



Shout it

blog comments powered by Disqus
hire me