考虑以下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>
这导致编译时出现以下错误:
如果我给ControlTemplate添加一个x:key属性,它就能编译了。 但是,我不应该这样做。 ControlTemplate装饰有DictionaryKeyProperty属性,该属性将TargetType指定为关键属性。 因此,只要我为ControlTemplate指定一个TargetType,我就不需要指定一个显式的键(类似于我定义的DataTemplate不需要指定键)。添加到IDictionary的所有对象都必须具有Key属性或某些其他类型的关联键。 行10,位置10。
我还有第二个与之相关的问题。 如果我在XAML中以这种方式定义ControlTemplate(无论是否指定了key),它是否会自动应用于所有未指定另一个模板的ContentControl类型的控件,或者我必须将ControlTemplate嵌入到Style中才能实现?
DictionaryKeyProperty
没有做它应该做的事情... - nemesv