WPF ResourceDictionary键属性“密钥”未找到

时间:2013-11-24 16:34:02

标签: wpf xaml tags

我创建了一个DateTimeConverter来将我的日期时间转换为MM / dd / yyyy格式。我尝试在我的xaml文件中使用它:

<Window.Resources>
    <converters:DateTimeConverter Key="DateTimeConverter"/>
</Window.Resources>

它导致错误说:在DateTimeConverter类型中找不到属性密钥。

然后经过一些谷歌搜索后,我将其改为:

<Window.Resources>
    <converters:DateTimeConverter x:Key="DateTimeConverter"/>
</Window.Resources>

它起作用(注意Key之前的“x:”)。

我的“x”默认定义为:xmlns:x =“http://schemas.microsoft.com/winfx/2006/xaml”

它现在正在运作,但我不知道为什么。有人可以请一些光吗?

1 个答案:

答案 0 :(得分:0)

向XAML对象元素添加x:Key值是识别资源字典中资源的最常用方法。

这是为WPF xaml选择的语法。 Key属性或属性不存在。

有关更多说明,请参阅MSDN规范。