为什么在XAML XmlDataProvider中我们必须说“x:Key”而不是“Key”?

时间:2009-01-23 15:05:22

标签: wpf xaml

命名空间的功能在这里是什么?

我想在这个简单的例子中我可以放“Key”和“XData”而不是“x:Key”和“x:XData”,但当我这样做时,在XmlDataProvider中找不到“Key”

<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>
...

2 个答案:

答案 0 :(得分:7)

“Key”属性不是XmlDataProvider的实际属性。它是一个Xaml属性,位于以“x”为前缀的“http://schemas.microsoft.com/winfx/2006/xaml”命名空间中。

StackPanel.Resources是一个ResourceDictionary。为了向字典添加内容,您需要一个键/值对。 Key属性是资源的键,XmlDataProvider是值。这是WPF用于唯一标识资源的机制。

您可以将xmlns:x =“http://schemas.microsoft.com/winfx/2006/xaml”更改为xmlns:ns =“http://schemas.microsoft.com/winfx/2006/xaml”和然后你会把它称为“ns:Key”而不是“x:Key”。

答案 1 :(得分:0)

您是否还从

中删除了:x
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

所以它读取

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