How To: RadMenuItem + ContainerBinding + CommandParameter as object?

4 posts, 0 answers
  1. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 19 Apr 2012 Link to this post

    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?
  2. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 19 Apr 2012 Link to this post

    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?


  3. DevCraft banner
  4. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 25 Apr 2012 Link to this post

    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 >>

  5. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 25 Apr 2012 Link to this post

    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.



Back to Top