【发布时间】:2012-03-21 21:56:30
【问题描述】:
考虑以下 XAML 文件:
<Window x:Class="ExpressionVisualizer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sle="clr-namespace:System.Linq.Expressions;assembly=System.Core"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type sle:BinaryExpression}"/>
<ControlTemplate TargetType="{x:Type ContentControl}"/>
</Window.Resources>
</Window>
这给出了以下编译错误:
添加到 IDictionary 的所有对象都必须具有 Key 属性或一些 与它们关联的其他类型的密钥。第 10 行位置 10。
如果我将 x:key 属性添加到 ControlTemplate,它会编译。但是,我不应该这样做。 ControlTemplate 使用 DictionaryKeyProperty 属性进行修饰,该属性将 TargetType 指定为关键属性。因此,只要我为我的 ControlTemplate 指定一个 TargetType,我就应该不必必须指定一个显式键(类似于我不必在我定义的 DataTemplate 上指定一个键)。
我有第二个与切线相关的问题。如果我在 XAML 中以这种方式定义 ControlTemplate(无论是否指定键),它是否会自动应用于未指定另一个模板的 ContentControl 类型的所有控件,或者我是否必须将 ControlTemplate 嵌入到样式中发生这种情况?
【问题讨论】:
标签: wpf xaml controltemplate resourcedictionary