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

How To: RadMenuItem + ContainerBinding + CommandParameter as object?

3 Answers 211 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.
Rob
Top achievements
Rank 1
Rob asked on 19 Apr 2012, 03:42 PM
Hello all,

I have a datagrid that lists a bunch of people and a context menu to perform some actions on those people - I'm using commands.

My context menu looks like this:
    
*
   -Add To Policy
          -Policy1   
          -Policy2
          -Policy3
   -Delete
   -Open
   -Blah Blah Blah

My problem is this.  When adding a person to a policy, I need to pass both the datagridrowcontext (a person) and the policy (a policy) to the command via containerbindings as a CommandParameter.

<telerik:HierarchicalDataTemplate x:Key="mnuPolicyTemplateContextMenu">
   <telerik:ContainerBinding.ContainerBindings>
      <telerik:ContainerBindingCollection>
         <telerik:ContainerBinding PropertyName="Command" Binding="{Binding Path=AddResourceToPolicyCommand, Source={StaticResource VMProxy}}" />
         <telerik:ContainerBinding PropertyName="CommandParameter">
            <telerik:ContainerBinding.Binding>
               <ps:AddResourceToPolicyCommandParameter Person="{Binding *datagridrow*}" Policy="{Binding *policy*}" />
            </telerik:ContainerBinding.Binding>
      </telerik:ContainerBinding>
   </telerik:ContainerBinding.ContainerBindings>
   <TextBlock Text="{Binding Name}" />
</telerik:HierarchicalDataTemplate>


1. How can I get a reference back to the datagridrow datacontext object (the person)?
2. Can I pass in an object like "ps:AddResourceToPolicyCommandParameter" with those parameters once I retrieve them?

3 Answers, 1 is accepted

Sort by
0
Rob
Top achievements
Rank 1
answered on 19 Apr 2012, 04:21 PM
I've answered question 1 --- 

<telerik:ContainerBinding PropertyName="CommandParameter">
    <telerik:ContainerBinding.Binding>
        <projectsecurity:AddResourceToPolicyCommandParameter ProjectResource="{Binding Path=Menu.DataContext, RelativeSource={RelativeSource Self}, Mode=TwoWay}" Policy="{Binding ., Mode=TwoWay}" />
    </telerik:ContainerBinding.Binding>
</telerik:ContainerBinding>


Now for question 2...  why do I get an System.Windows.Markup.XamlParseException when trying to create this object?


0
Ivo
Telerik team
answered on 25 Apr 2012, 08:50 AM
Hi,

I think what you are trying cannot be achieved this way. I would suggest you to use the RadContextMenu's opened event. There you can get the clicked element.
private void RadContextMenu_Opened( object sender, RoutedEventArgs e )
{
    var item = this.radContextMenu.GetClickedElement<DataGridRow>();
     //update the last selected item into your viewModel
}

Then you will have to pass only one value into your CommandParameter which will be a lot easier task. I understand you don't want to use code behind, but into this case this will be the recommended solution.

Kind regards, Ivo
the Telerik team

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

0
Rob
Top achievements
Rank 1
answered on 25 Apr 2012, 01:00 PM
Sorry, I should have closed this...

<telerik:ContainerBinding PropertyName="CommandParameter" Binding="{Binding ., RelativeSource={RelativeSource Self}, Converter={StaticResource AddResourceToPolicyCommandParameterConverter}}" />

I figured out a way around it by binding to the RadMenuItem, and creating the parameter object in a converter...  Still hacky, but much better than code behind.



Tags
Menu
Asked by
Rob
Top achievements
Rank 1
Answers by
Rob
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or