Unable to set Command property on RadMenuItem with ContainerBinding

5 posts, 1 answers
  1. Nathanael
    Nathanael avatar
    4 posts
    Member since:
    Dec 2010

    Posted 22 Sep 2011 Link to this post

    Hi folks!
    I'm following the example with Menu & DataBinding:
    <telerik:ContainerBinding PropertyName="Header" Binding="{Binding Converter={StaticResource PnlWorkflowStatusToLabelConverter}}" />
    <telerik:ContainerBinding PropertyName="Command" Binding="{Binding ElementName=LayoutRoot, Path=DataContext.GoToStep, Converter={StaticResource DebugConverter}, Mode=OneWay}" />
    <telerik:ContainerBinding PropertyName="CommandParameter" Binding="{Binding Converter={StaticResource DebugConverter}}" />
    <telerik:RadMenuItem Header="Go to step:" ItemsSource="{Binding Path=AvailableStatus}">
            <telerik:HierarchicalDataTemplate telerik:ContainerBinding.ContainerBindings="{StaticResource StatusContainerBindings}">
                <TextBlock Text="{Binding}" />
    It works fine for Header, for CommandParameter but not for Command.
    When I step into the sources of ContainerBinding, I can see that the Apply(FrameworkElement container) is called, the container.SetBinding is executed, this.Binding is alright, property is not null, container is my RadMenuItem. But altough everything looks correct, when it's done on Command property, the property stays at null after whereas on CommandParameter, it's successfull!

    Am I missing something?
    Best regards,
  2. Nathanael
    Nathanael avatar
    4 posts
    Member since:
    Dec 2010

    Posted 22 Sep 2011 Link to this post

    It seems that the probleme is coming from the ElementName which is not well behaving.
    NB: I'm on 2011.2.912.1040
  3. Answer
    Valeri Hristov
    Valeri Hristov avatar
    2252 posts

    Posted 27 Sep 2011 Link to this post

    Hello Nathanael,

    You are correct, the ElementName bindings do not work in RadContextMenu due some limitations in Silverlight (the control has to be in the visual tree in order the bindings to work). I suppose the best you could do is to bind to the Menu.UIElement property of the RadMenuItem using RelativeSource=Self (this is the element RadContextMenu is attached to) and get the DataContext from there.

    Best wishes,
    Valeri Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  4. Nathanael
    Nathanael avatar
    4 posts
    Member since:
    Dec 2010

    Posted 27 Sep 2011 Link to this post

    Great! Works fine! :)
    I was using a workaround (wrapping my object in a kind of Tuple<MyObject,ICommand>) but your solution works fine.
    Thanks a lot!
  5. Joshua
    Joshua avatar
    1 posts
    Member since:
    Nov 2011

    Posted 09 Jul 2012 Link to this post

    To get around this, reference the ViewModel context in the RadContextMenu header like this:

    Given: You have a command in your viewmodel referenced as: MyViewModel.MyCommand. In this case, the ItemsSource reference is a peer to MyCommand, so there is no hierarchical relationship between them.

        <telerik:RadContextMenu DataContext="{Binding Path=MyViewModel}">
            <telerik:RadMenuItem Header="Do Stuff"
                Command="{Binding Path=MyCommand}">
Back to Top