Odeslat dotazOdeslat dotaz
 

DotazDatabound MenuItem's parent is null.

  • 22. října 2009 12:09Neil Moore . Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
    Hi,

    I'm writing a ContextMenu where some of the menu is hard coded and some of it comes from a databound source.  The context menu itself is a resource on the window as it needs to be applied to several controls via a style.  The XAML for the menu looks something like this.

    <ContextMenu x:Key="MyContextMenu">
      <ContextMenu.DataContext>
        <ViewModel:MenuVM>
      </ContextMenu.DataContext>
    
      <MenuItem Header="Menu 1" />
      <MenuItem Header="Menu 2" />
      <MenuItem Header="My Sub Menus" ItemsSource="{Binding SubItems}">
        <MenuItem.ItemContainerStyle>
          <Style TargetType="{x:Type MenuItem}">
            <EventSetter Event="Click" Handler="MenuItem_Click" />
          </Style>
        </MenuItem.ItemContainerStyle>
      </MenuItem>
    </ContextMenu>
    
    This works find and I get the menu looking as I'd expect, my problems come when I handle the mouse click and I want to find my way back to the ContextMenu from the source property of the event args to use the PlacementTarget property.  On the menu items that have been created from the data source, the parent property is null.  Now looking in the debugger I can see that the LogicalParent which is a private property has a value of the parent menu item, so I tried using the LogicalTreeHelper.GetParent and that also returns null.

    Is this something anyone else has come across or knows a way around ?

    Many thanks in advance,

    Neil.

Všechny reakce

  • 22. října 2009 23:30Bigsby Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Try using VisualTreeHelper.GetParent(menuItem). They might not be set as logical children but if they're there they must be visual children.
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
  • 23. října 2009 2:59Neil Moore . Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Hi,

    Thanks for responding.  I tried that also, and there are some parent visual elements, but going through the visual tree returns StackPanel -> ItemsPresenter -> ScrollContentPresenter -> Border -> Grid -> ScrollViewer -> ContentPresenter etc... and finishes in a Primitives.PopupRoot before becoming null.
  • 4. listopadu 2009 17:14Neil Moore . Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Ok, I've done some more double checking on this, if I manually add a menu tree in XAML and then check the VisualTreeHelper.GetParent that is the same as the databound one and only returns the visual items that make up the menu.  Looking at the the LogicalTreeHelper.GetParent on the manually added element it returns the parent menu item as I would expect, but on the one added via a databinding it returns null.

    This proves two things, firstly that the item I am looking for should be the logical parent, and that there is obviously an issue with the parent when the items are generated from a data source.

    Of course this still leaves me with no clue as to how to resolve my issue.