为什么绑定到附加属性在这种情况下不起作用。
我希望能够将ContentPresenter的ContentTemplate属性绑定到附加属性范围:ContextMenuExtension.HeaderDataTemplate。在下面的xaml中,我在样式设置器中设置了该属性,以便缩短代码。我也试过在ContextMenu对象上设置它。每次我收到错误:
BindingExpression path error: '(extent:ContextMenuExtension.HeaderDataTemplate)' property not found on 'object' ''ContextMenu' (Name='')'. BindingExpression:Path=(extent:ContextMenuExtension.HeaderDataTemplate); DataItem='ContextMenu' (Name=''); target element is 'ContentPresenter' (Name=''); target property is 'ContentTemplate' (type 'DataTemplate')
XAML:
<Style TargetType="{x:Type ContextMenu}" >
<Setter Property="extent:ContextMenuExtension.HeaderDataTemplate" Value="{StaticResource DataTemplate}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Grid>
<AdornerDecorator>
<Border Margin="8" Padding="0 8 0 8" Background="White" Effect="{DynamicResource MDShadow2}"/>
</AdornerDecorator>
<Border Margin="8" Padding="0 8 0 8">
<StackPanel Orientation="Vertical">
<Grid HorizontalAlignment="Stretch" Margin="0 -8 0 0">
<ContentPresenter ContentTemplate="{Binding (extent:ContextMenuExtension.HeaderDataTemplate), RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:2)
绑定到附加属性时,请添加Path=
<ContentPresenter ContentTemplate="{Binding Path=(extent:ContextMenuExtension.HeaderDataTemplate), RelativeSource={RelativeSource TemplatedParent}}" />