2017-01-03 98 views
1

爲什麼綁定到附屬屬性在這種情況下不起作用。爲什麼綁定到附加屬性不起作用?

我希望能夠將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> 

回答

2

當綁定到一個附加屬性,加Path=

<ContentPresenter ContentTemplate="{Binding Path=(extent:ContextMenuExtension.HeaderDataTemplate), RelativeSource={RelativeSource TemplatedParent}}" />