Consider the following XAML file:
<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>
This results in the following compilation error:
All objects added to an IDictionary must have a Key attribute or some other type of key associated with them. Line 10 Position 10.
If I add the x:key
attribute to the ControlTemplate, it compiles. However, I should not have done this. The ControlTemplate is decorated with the DictionaryKeyProperty attribute, which defines TargetType as a key property. As long as I specify a TargetType for my ControlTemplate, I must not specify an explicit key (similar to how I should not specify one of the specified DataTemplate).
I have a second and tangent question. If I define a ControlTemplate this way (either by specifying a key or not) in XAML, does it automatically apply to all controls like ContentControl that don't specify another template, or will I need to insert a ControlTemplate inside the style for this?
source share