superstator
superstator

Reputation: 3228

MenuItem databinding

I've been puzzling over this for a day or so with no luck - I'm probably missing something obvious. Basically, I have a context menu with two items. One is statically declared, and bound to a command. The other has no command of its own, but binds to a collection of viewmodels. So visually the menu should look something like:

Where the items vary depending on the thing the context menu was bound to. Originally I had something like this:

    <ContextMenu x:Key="itemContextMenu">
        <MenuItem Header="_Delete"
                  Command="{Binding DeleteCommand}" />
        <MenuItem Header="_Add" DataContext=""
                  ItemsSource="{Binding AvailableTypes}">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Path=ItemType.Name}"
                              Command="{Binding Path=AddItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
                              CommandParameter="{Binding}" />
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
    </ContextMenu>

Which works, but gives me the nested MenuItems others have experienced. Based on a couple threads here on stackoverflow, I then tried this:

    <ContextMenu x:Key="itemContextMenu">
        <MenuItem Header="_Delete"
                  Command="{Binding DeleteCommand}" />
        <MenuItem Header="_Add" 
                  ItemsSource="{Binding AvailableTypes}">
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">                        
                    <Setter Property="Header"
                            Value="{Binding Path=ItemType.Name}" />
                    <Setter Property="Command"
                            Value="{Binding Path=AddItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
                    <Setter Property="CommandParameter"
                            Value="{Binding}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </ContextMenu>

However, when I do that my bindings all fail with errors like:

BindingExpression path error: 'ItemType' property not found on 'object' ''String'

BindingExpression path error: 'AddItemCommand' property not found on 'object' ''Grid'

To me that says that the DataContext is getting lost when I use the ItemContainerStyle. What am I missing?

edit:

I think I had some red herrings in here, so I've simplified the examples further to try and narrow down the problem.

Working but screwy layout-wise:

    <ContextMenu x:Key="itemContextMenu">
        <MenuItem Header="_Delete" />
        <MenuItem Header="_Add" DataContext=""
                  ItemsSource="{Binding AvailableTypes}">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Path=ItemType.Name}" />
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>
    </ContextMenu>

Non-working with a BindingExpression error:

    <ContextMenu x:Key="itemContextMenu">
        <MenuItem Header="_Delete" />
        <MenuItem Header="_Add" 
                  ItemsSource="{Binding AvailableTypes}">
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">                        
                    <Setter Property="Header"
                            Value="{Binding Path=ItemType.Name}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </ContextMenu>

Upvotes: 3

Views: 3245

Answers (1)

superstator
superstator

Reputation: 3228

Apparently this is a bug in 3.5. I upgraded my project to 4.0, and now everything works as expected.

Upvotes: 1

Related Questions