Search code examples
c#wpfxamldata-bindingattached-properties

Binding AttachedProperty of Collection type to TemplatedParent


I want to create an attached property to hold a collection of MenuItem objects. These are to be used in my custom ControlTemplate for GroupBoxes. In that ControlTemplate I want to use my custom DropDownButton which inherits from ItemsControl and will put the MenuItems inside its Popup.

I found tips on this site:
https://siderite.dev/blog/collection-attached-properties-with.html

Here's what I have:

The AttachedProperty:

public class General {
    public static readonly DependencyProperty MenuItemsProperty =
        DependencyProperty.RegisterAttached(
            "MenuItemsInternal",from convention
            typeof(ObservableCollection<MenuItem>), 
            typeof(General),
            new PropertyMetadata(default(ObservableCollection<MenuItem>)));

    public static ObservableCollection<MenuItem> GetMenuItems(UIElement element)
    {
         var collection = (ObservableCollection<MenuItem>) element.GetValue(MenuItemsProperty);
         if (collection == null)
         {
             collection = new ObservableCollection<MenuItem>();
             element.SetValue(MenuItemsProperty, collection);
         }
         return collection;
    }

    public static void SetMenuItems(UIElement element, ObservableCollection<MenuItem> value)
    {
        element?.SetValue(MenuItemsProperty, value);
    }
}

Usage of the AttachedProperty:

...
<GroupBox Style="{StaticResource Style.GroupBox.EditableSubSection}">
    <ap:General.MenuItems>
        <MenuItem Header="Aktion abc" />
        <MenuItem Header="Aktion xyz" />
    </ap:General.MenuItems>
</GroupBox>

So far so good. This all works. My problem is that I cannot find a way to use the collection of MenuItems in my ControlTemplate for the GroupBox.

 <Style x:Key="Style.GroupBox.EditableSubSection"
        TargetType="{x:Type GroupBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="{TemplateBinding Header}" />
                    <Separator Grid.Row="1" />
                    <ContentPresenter Grid.Row="2" />
                    <Grid Grid.Row="0" HorizontalAlignment="Right">
                        ...
                        <controls:DropDownButton Width="{Binding ActualHeight,
                                                                 RelativeSource={RelativeSource Self}}"
                                                 ItemsSource="{Binding Path=(ap:General.MenuItems),
                                                                       RelativeSource={RelativeSource TemplatedParent}}" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

When running this the problem seems to be the binding

 ItemsSource="{Binding Path=(ap:General.MenuItems), RelativeSource={RelativeSource TemplatedParent}}"

I get an Exception with the following message:

InvalidOperationException: Property path is not valid. 'General' does not have a public property named 'MenuItems'

Has anyone experienced this before or any tips on how to bind to an AttachedProperty with non-conventional name?


Solution

  • @Jinish provided an answer in the comments.

    Your property name as I can see from the code is "MenuItemsInternal". That't the name that will be used for WPF binding purpose as far as I am aware

    Changing the binding to the following solves the problem

    ItemsSource="{Binding Path=(ap:General.MenuItemsInternal), RelativeSource={RelativeSource TemplatedParent}}"
    

    Since there is no way to convert a comment into an answer, I'm answering my own question quoting the comment as suggested on meta: https://meta.stackexchange.com/a/1558