DataContext binding ignored

4 posts, 1 answers
  1. David
    David avatar
    46 posts
    Member since:
    Nov 2008

    Posted 20 Nov 2012 Link to this post

    DataConext binding for my RadContextMenu is being ignored. My menu is defined like this:

    <UserControl.Resources>
        <telerik:RadContextMenu x:Key="LimbContextMenu" DataContext="{Binding SelectedLimb}" >
            <telerik:RadMenuItem Header="Expand" Command="{Binding ExpandItemCommand}" />
            <telerik:RadMenuItem Header="Expand to book" Command="{Binding ExpandToBookCommand}" />
            <telerik:RadMenuItem Header="Collapse" Command="{Binding CollapseItemCommand}" />
            <telerik:RadMenuItem Header="Collapse all" Command="{Binding CollapseAllCommand}" />
            <telerik:RadMenuItem Header="Jump to home" Command="{Binding JumpToHomeCommand}"
                                 Visibility="{Binding JumpToHomeCommand.IsSupported, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </telerik:RadContextMenu>
    </UserControl.Resources>


    It works if I define it like this:
    <UserControl.Resources>
        <telerik:RadContextMenu x:Key="LimbContextMenu"   >
          <telerik:RadContextMenu DataContext="{Binding SelectedLimb}">
            <telerik:RadMenuItem Header="Expand" Command="{Binding ExpandItemCommand}" />
            <telerik:RadMenuItem Header="Expand to book" Command="{Binding ExpandToBookCommand}" />
            <telerik:RadMenuItem Header="Collapse" Command="{Binding CollapseItemCommand}" />
            <telerik:RadMenuItem Header="Collapse all" Command="{Binding CollapseAllCommand}" />
            <telerik:RadMenuItem Header="Jump to home" Command="{Binding JumpToHomeCommand}"
                                 Visibility="{Binding JumpToHomeCommand.IsSupported, Converter={StaticResource BooleanToVisibilityConverter}}"/>
          </telerik:RadContextMenu>
        </telerik:RadContextMenu>
    </UserControl.Resources>

    But this version displays a menu within a menu.

    How can I get DataConext assigned on the outer menu?
  2. Answer
    Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 21 Nov 2012 Link to this post

    Hi David,

    By default RadContextMenu inherits the DataContext of the UIElement on which it is attached. If you want to set DataContext, you have to use InheritDataContext property and set it to false (the default value is true).
    <UserControl.Resources>
    <telerik:RadContextMenu x:Key="LimbContextMenu" DataContext="{Binding SelectedLimb}" InheritDataContext="False">
    ...

    Don't hesitate to contact us if you have any problems or concerns.

    Greetings,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. David
    David avatar
    46 posts
    Member since:
    Nov 2008

    Posted 26 Nov 2012 Link to this post

    Thanks. That worked fine. I like it when the solution is easy!
  5. Mahdi
    Mahdi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 11 Jun 2014 Link to this post

    Thanks Rosen Vladimirov. It's easy and practical.
Back to Top