This is a migrated thread and some comments may be shown as answers.

Unable to set Command property on RadMenuItem with ContainerBinding

4 Answers 309 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nathanael
Top achievements
Rank 1
Nathanael asked on 22 Sep 2011, 05:38 PM
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:RadMenuItem.ItemTemplate>
        <telerik:HierarchicalDataTemplate telerik:ContainerBinding.ContainerBindings="{StaticResource StatusContainerBindings}">
            <TextBlock Text="{Binding}" />
        </telerik:HierarchicalDataTemplate>
    </telerik:RadMenuItem.ItemTemplate>
</telerik:RadMenuItem>
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,

4 Answers, 1 is accepted

Sort by
0
Nathanael
Top achievements
Rank 1
answered on 22 Sep 2011, 05:48 PM
It seems that the probleme is coming from the ElementName which is not well behaving.
NB: I'm on 2011.2.912.1040
0
Accepted
Valeri Hristov
Telerik team
answered on 27 Sep 2011, 02:49 PM
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 >>

0
Nathanael
Top achievements
Rank 1
answered on 27 Sep 2011, 03:55 PM
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!
Regards,
0
Joshua
Top achievements
Rank 1
answered on 09 Jul 2012, 04:17 PM
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.ContextMenu>
    <telerik:RadContextMenu DataContext="{Binding Path=MyViewModel}">
        <telerik:RadMenuItem Header="Do Stuff"
            Command="{Binding Path=MyCommand}">
        </telerik:RadMenuItem>
    </telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
Tags
Menu
Asked by
Nathanael
Top achievements
Rank 1
Answers by
Nathanael
Top achievements
Rank 1
Valeri Hristov
Telerik team
Joshua
Top achievements
Rank 1
Share this question
or