Vlad
Vlad

Reputation: 878

Why binding to attached property doesn't work?

Why binding to attached property doesn't work in this case.

I want to be able to bind ContentTemplate property of ContentPresenter to attached property extent:ContextMenuExtension.HeaderDataTemplate. In xaml below I've set that property in style setter in order to make code shorter. I've tried to set it on ContextMenu object as well. Each time I'm getting an error:

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>

Upvotes: 1

Views: 142

Answers (1)

d.moncada
d.moncada

Reputation: 17402

When binding to an attached property, add Path=

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

Upvotes: 2

Related Questions