Friday, October 16, 2009

Silverlight 3 XamlWriter - v0.4 - DataTemplate support

The new version of XamlWriter with DataTemplate support is now available. Check the previous posts for download and usage information.

While it is possible to recreate the DataTemplate it is not possible to recreate ContentTemplate. Hence I couldn't add support for ContentTemplate. There is also support for the Style class but there is an issue with it.

The DataTemplate example



Assuming an Xaml like this

<UserControl x:Class="test2.MainPage" x:Name="MyControl"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    Width="1000" Height="300">
    <Border BorderBrush="Green" BorderThickness="4">
        <Grid x:Name="LayoutRoot" Background="White"  >
            <controls:TreeView x:Name="trv" ItemsSource="{Binding}" Width="500" BorderThickness="4" BorderBrush="Purple">
                <controls:TreeView.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Blue" BorderThickness="4">
                            <Grid>
                                <Border BorderBrush="Yellow" BorderThickness="4">
                                    <TextBlock Text="Hello"></TextBlock>
                                </Border>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </controls:TreeView.ItemTemplate>
            </controls:TreeView>
            <Button x:Name="zz" Width="100" Height="100" Click="Button_Click"/>
        </Grid>
    </Border>
</UserControl>


WriteXaml(MyControl, XamlWriterSettings.LogicalTree) will generate this Xaml


<UserControl x:Name="MyControl" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/client/2007">
  <Border BorderThickness="4,4,4,4" BorderBrush="#FF008000">
    <Grid x:Name="LayoutRoot" Background="#FFFFFFFF">
      <TreeView BorderThickness="4,4,4,4" Width="500" x:Name="trv" BorderBrush="#FF800080">
        <TreeView.ItemTemplate>
          <DataTemplate>
            <Border BorderThickness="4,4,4,4" BorderBrush="#FF0000FF">
              <Grid>
                <Border BorderThickness="4,4,4,4" BorderBrush="#FFFFFF00">
                  <TextBlock Text="Hello" />
                </Border>
              </Grid>
            </Border>
          </DataTemplate>
        </TreeView.ItemTemplate>
      </TreeView>
      <Button Width="100" Height="100" x:Name="zz" />
    </Grid>
  </Border>
</UserControl>

5 comments:

test said...

Very good job but your assembly is not compatible with Visual Studio 2010 beta 2. Can you fix it please ?
thx

Wilfred Pinto said...

I will look into it as soon as I get access to a machine with Visual Studio 2010.

David Price said...

Fantastic. I'm using it to build a WYSIWYG editor for my project.

For some reason it doesn't output the TextDecorations property on the TextBlock element. Are you able to put this in the next version?

Everything else i've tried works perfectly, thanks again!

Anonymous said...

would be great, if it also support Path.Data...

but it is the best xamlwriter i have seen till now

Wilfred Pinto said...

David,

Thanks for the feedback. I will look into the TextDecorations property and Path.Data element as soon as I get some time.

Please continue to post the issues as and when you encounter them.

Thanks

Wilfred