Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

WPF MenuItem + DataTemplate = strangeness

Back to Forum: Tech Off
  • User profile image
    spivonious

    I've gotten a little free time at work, so it's back to playing with WPF. I'm trying to dynamically populate a menu bar based on entries from a database. I made a custom class (MyMenuItem) that has a name and a collection of MyMenuItem objects called SubMenus.

    I made a WPF control called MyMenuControl that takes a MenuItem and binds Header to Name and ItemsSource to SubMenus.

    In my main window, I have DataTemplate for all MyMenuItem objects that tells it to use the MyMenuControl to display. I have a Menu control for containing the items.

    In the code-behind of my main window, I load in the menu items from the database, and add the top items to the Menu control's Items collection.

     

    So far so good, right? I run the program and get the top level menus in the menu bar. Three odd things are happening:

    1. The menu items show their name but also show an arrow as if they had a pop-out menu. I want them to appear as normal menu items.

    2. The menu items all have a fixed width instead of sizing around their name. Width is not set in MyMenuControl so I'm not sure why this is happening.

    3. When I click a menu, it gets selected. To click something else I have to click twice - once to unselect the menu and again to click the other control.

     

    Does anyone have any ideas? I feel like I'm missing something obvious.

  • User profile image
    spivonious

    *bump*

    Anyone? I know there aren't many WPF devs on here, but I would think this would be a standard thing in most apps.

  • User profile image
    vesuvius

    It is (standard), so I would say code sample required.

  • User profile image
    vesuvius

    It is (standard), so I would say code sample required.

  • User profile image
    spivonious

    <DataTemplate DataType="{x:Type local:ParallaxMenuItem}">
         <MenuItem Header="{Binding Name}" ItemsSource="{Binding SubItems}"></MenuItem>
    </DataTemplate>
     

     

    The SubItems collection in the ParallaxMenuItem class is an ObservableCollection<ParallaxMenuItem>.

    Let me know if you want to see any other code.

    An odd thing - if I press the spacebar while a menu is selected, it always opens the system menu (as if I had pressed Alt-Space). Something fishy is going on.

  • User profile image
    spivonious

    Ah, I fixed it. I needed to use a HierarchicalDataTemplate. The selection problems went away too.

    In case anyone comes across this thread in the future, here's the code that worked:

      <HierarchicalDataTemplate DataType="{x:Type local:ParallaxMenuItem}" ItemsSource="{Binding Path=SubMenus}"><ContentPresenter Content="{Binding Path=Name}" /> </HierarchicalDataTemplate>

     

  • User profile image
    spivonious

    Of course, now I need to bind the Command and find no Command property on my ContentPresenter.

    I did find the cause of my issue before - a MenuItem is automatically created with an ItemTemplate specified by the DataTemplate, so I was creating a MenuItem inside of a MenuItem, which caused the funky selection behavior.

    Now I just need something like a Button that just displays the text without the button graphic. ControlTemplate?

    WPF has some really really neat parts, but then you run into these things that make you wonder what the team was thinking.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.