Share via


A DataGrid sample using XML data

I love LINQ, so I'm on a mission to use it in as many samples as possible.  This one is pretty trivial but it took me a bit to put together all the pieces of loading up the xml file to getting the binding to recognize the correct attributes.  Finally I stylized the DataGrid a bit by using a HeaderStyle.

A few notes:

  1. You need the WPF Toolkit.  I'm using Visual Studio 2008 SP1.
  2. Add the WPF Toolkit dlls as resources.
  3. Add the xml file as an embedded resource.

Here's the code:

XAML

<Window x:Class="DataGridXML.Window1"

   xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:toolkit="https://schemas.microsoft.com/wpf/2008/toolkit"

   Title="Window1" Height="300" Width="300">

    <Window.Resources>

        <Style TargetType="toolkit:DataGridColumnHeader" x:Key="MyHeaderStyle">

            <Setter Property="FontSize" Value="14"/>

            <Setter Property="Foreground" Value="Blue"/>

        </Style>

    </Window.Resources>

    <Grid>

       

        <toolkit:DataGrid Name="dg1" ItemsSource="{Binding}"  >

            <toolkit:DataGrid.Columns>

                <toolkit:DataGridTextColumn Header="Album Title" Binding="{Binding Path=Attribute[Title].Value}" HeaderStyle="{StaticResource MyHeaderStyle}" />

                <toolkit:DataGridTextColumn Header="Artist" Binding="{Binding Path=Attribute[Artist].Value}" HeaderStyle="{StaticResource MyHeaderStyle}"/>

                <toolkit:DataGridTextColumn Header="Release Date" Binding="{Binding Path=Attribute[ReleaseDate].Value}" HeaderStyle="{StaticResource MyHeaderStyle}" />

            </toolkit:DataGrid.Columns>

        </toolkit:DataGrid>

    </Grid>

</Window>

CS (not the whole file, just add to the default Window1.xaml.cs file)

//add using statements for LINQ

using System.Xml;

using System.Xml.Linq;

public Window1()

        {

            InitializeComponent();

            //Load XML file and set the DataGrid data context

            XElement mymusic = XElement.Load("mymusic.xml");

            dg1.DataContext = mymusic.Elements("Album");

           

        }

VB (not the whole file, just add to the default Window1.xaml.vb file)

'add using statements for LINQ

Imports System.Xml

Imports System.Xml.Linq

Class Window1

    Public Sub New()

        ' This call is required by the Windows Form Designer.

        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        'Load XML file and set the DataGrid data context

        Dim mymusic As XElement = XElement.Load("mymusic.xml")

        dg1.DataContext = mymusic.Elements("Album")

    End Sub

End Class

XML (called mymusic.xml)

<Music>

  <Album Title="Chris Sells Live" Artist="Chris Sells" ReleaseDate="2/5/2008" />

  <Album Title="The Road to Redmond" Artist="Luka Abrus" ReleaseDate="4/3/2008" />

  <Album Title="The Best of Jim Hance" Artist="Jim Hance" ReleaseDate="6/2/2008" />

</Music>

 

I used the following documentation which you might also find useful for more complex scenarios.

Margaret

Comments