views:

1785

answers:

4

Hi, I am using an ObjectDataProvider and a DataTemplate to populate a MenuItem inside my Menu bar. (WPF, C#/XAML) See snipet below.

Result: The top menu item appears, when i click on it, the wrapping menu item (the one with the bound header text) appears along with the little arrow indicating the presence of children but hovering or clicking the arrow does not show the children, they cannot be accessed.

Expected result: The children are visible and behave properly.

Snippet:

<ObjectDataProvider x:Key="Brokers" ObjectInstance="{x:Static brokers:BrokerManager.Instance}" MethodName="GetBrokers" IsAsynchronous="True" />
        <DataTemplate x:Key="BrokerMenuItem" DataType="IBroker">
            <MenuItem Header="{Binding Path=Name}">
                <MenuItem Header="Connect" />
                <MenuItem Header="Disconnect" />
            </MenuItem>
        </DataTemplate>

<MenuItem Header="Brokers" ItemsSource="{Binding Source={StaticResource Brokers}}" ItemTemplate="{DynamicResource BrokerMenuItem}"/>
A: 

ItemSource property of menuitem control is used for giving childes for that item, try to use <ContentPresenter /> with that datatemplate.

ArsenMkrt
A: 

arsenmrkt: I have exactly the same problem, if I populate a MenuItem using a DataTemplate I cant seem to add children to any of those generated items. I don't understand your answer though, how should I use the ContentPresenter to get around this problem?

EDIT: Actually, my problem was'nt exactly the same, since I'm trying to bind a collection of collections to a menu. I think I've gotten it to work though using the HierarchicalDataTemplate:

<Menu>
    <MenuItem Header="{Binding Name}" ItemsSource="{Binding MenuOptions}">
        <MenuItem.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Categories}">
                <MenuItem Header="{Binding Name}"/>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding Name}"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>
</Menu>

Does this help you NicholasF?

scim
A: 

After searching for over a week, i finally found how to make this work properly. It turns out DataTemplates don't work too great for dynamic menus. The proper way to do this is to use the ItemContainerStyle property of MenuItem. (Or is that ItemStyleContainer?)

Simply create a style to override the header and set it to whatever you need. I them overrode the ItemsSource to include my children. However be careful here, as the children will inherit the style and each have the same children and generate a recursive menu. You'll need to override the ItemsSource of your children and set it to an empty x:Array or the likes.

There are several blogs out there describing how to use ItemContainerStyle, check them out.

+1  A: 

I had a similar problem. This blog article showed me how to get my data-driven menus working:

http://weblogs.asp.net/okloeten/archive/2007/11/14/5149692.aspx

Ashley Davis