tags:

views:

529

answers:

2

What is the function of the namespace here?

I would think in this simple example I would be able to put "Key" and "XData" instead of "x:Key" and "x:XData" but when I do it says "Key" was not found in XmlDataProvider.

<Window x:Class="DataBindingWPF.XmlBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DataBindingWPF" Height="300" Width="300"
    >
    <StackPanel>
     <StackPanel.Resources>
      <XmlDataProvider x:Key="Colors" Source="Colors.xml"  XPath="/colors"></XmlDataProvider>
      <XmlDataProvider x:Key="MoreColors" XPath="/colors">
       <x:XData>
...
A: 

Did you also remove the :x from

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

so it reads

xmlns="http://schemas.microsoft.com/winfx/2006/xaml"
James Curran
yes, then I still get the same error, same as when you take the "x:" off of Class, the it says "Class is not a property of Window"
Edward Tanguay
+6  A: 

The "Key" property is not an actual property of the XmlDataProvider. It's a Xaml attribute that lives in the "http://schemas.microsoft.com/winfx/2006/xaml" namespace you prefixed with "x".

StackPanel.Resources is a ResourceDictionary. In order to add something to a dictionary you need a Key/value pair. The Key attribute is the key for the resource, and the XmlDataProvider is the value. This is the mechanism WPF uses to uniquely identify resources.

You can change xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" to xmlns:ns="http://schemas.microsoft.com/winfx/2006/xaml" and then you would refer to it as "ns:Key" instead of "x:Key".

Micah
good concise answer +1
discorax