views:

27

answers:

1

After a few hours trying to reach some working sample i decided to try my luck here. I am a newbie to WPF, but things should get easier with time ...

I am trying to have a DataGrid showing an XML file i have. That's it.

I have in my application cars.xml

  <cars>
    <car type="Ford" size="4" />
    <car type="Mercedes" size="2" />
    <car type="BMW" size="1" />
  </cars>

Now i have a UserControl which have a DataGrid (using .NET 3.5 SP1 on VS2008 with CodePlex ToolKit):

   <dg:DataGrid ItemsSource="{Binding cars}" />

As i understands, since it have AutoColumn it should show me a grid with the cars ... but it's not. I guess my error is with the Binding. I haven't got this Binding idea so good in WPF but i am learning. So how do i point the ItemSource to my cars.xml correctly?

10x.

+1  A: 

You must specify your xml file as as resource of you grid, or of you window. For example:

<Window.Resources>
<XmlDataProvider XmlNamespaceManager="{StaticResource ns}" x:Key="rss" Source="http://weather.yahooapis.com/forecastrss?p=RSXX1410&amp;amp;u=c" XPath="/rss/channel" />
...

Static Resource ns -it's a namespace for different prefixes -if you have it in your xml:

<XmlNamespaceMappingCollection x:Key="ns">
        <XmlNamespaceMapping Prefix="yweather" Uri="http://xml.weather.yahoo.com/ns/rss/1.0" />
        <XmlNamespaceMapping Prefix="geo" Uri="http://www.w3.org/2003/01/geo/wgs84_pos#" />
    </XmlNamespaceMappingCollection>
...
</Window.Resources>

Now you can make binding your xaml elements to your xml-elements:

<Grid DataContext="{Binding Source={StaticResource rss}, XPath=item}">
     <Image  Width="200" Height="180" Source="{Binding XPath=yweather:condition/@code, Converter={StaticResource WeatherCodeToImageConverter}}" />
</Grid>

Thats it.

Alex