Skip to content

Good Progress on Xaml to Code Tool

March 22, 2007

[update 6/10/2007: I’ve posted a version of this Xaml to Code tool on  Select sample.xaml from the right side of the home page of the application and choose open.]

I mentioned last week that I was building a Xaml to Code converter.


It can now convert this Xaml:

<Window x:Class="xamlU.Window1"
   Title="xamlU" Height="471" Width="665" WindowState="Maximized">
       <RowDefinition Height="*" />
        <RowDefinition Height="*" />
   <Button IsDefault="True">Ok</Button>
   <Button Grid.Row="1">Cancel</Button>

To this C#:

            Window Window1 = new Window();
            Window1.Title = "xamlU";
            Window1.Height = ((Double)new LengthConverter().ConvertFromString("471"));
            Window1.Width = ((Double)new LengthConverter().ConvertFromString("665"));
            Window1.WindowState = WindowState.Maximized;

            Grid Grid1 = new Grid();
            Window1.Content = Grid1;

            RowDefinition RowDefinition1 = new RowDefinition();
            RowDefinition1.Height = ((GridLength)new GridLengthConverter().ConvertFromString("*"));

            RowDefinition RowDefinition2 = new RowDefinition();
            RowDefinition2.Height = ((GridLength)new GridLengthConverter().ConvertFromString("*"));

            Button Button1 = new Button();
            Button1.IsDefault = true;

            Button1.Content = "Ok";

            Button Button2 = new Button();
            Grid.SetRow(Button2, 1);

            Button2.Content = "Cancel";

Required Enhancements

  • Handle Markup Extensions
  • Consider how x:Class should be handled
  • Support VB
  • Definitely more to think of and list here…


Possible Enhancements

  • Share instances of TypeConverters, instead of creating a new one for each property set.
  • Avoid using TypeConverters at all…figure out the object created from the type converter and convert it to code.

From → XAML (non-UI)

  1. Unknown permalink

    Grate idea!
    My main challenge is converting Markup Extensions to C# code.  Something like this
    <TextBox Text   ="{Binding Source={StaticResource str} Path=Text }"/>

  2. Rob permalink

    Yep, mapping some markup extensions to code will be a challenge…
    You can start by creating the markupextension and calling ProvideValue on it from code…but that won\’t always work as some rely on a ServiceProvider passed into ProvideValue…

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: