How to bind RadMenuItem to a command of parent model and pass current item to parameter?

3 posts, 0 answers
  1. Bill
    Bill avatar
    5 posts
    Member since:
    Dec 2019

    Posted 22 Jan Link to this post


    I have some struggle with context menu binding. The problem is that I cant bind Command from  CustomerViewModel  to a RadMenuItem in a Context menu.

    Here is my code:

    public class CustomerViewModel : ViewModelBase
        private ObservableCollection<Customer> _customers;
        public ICommand AddCom { get; set; }
        public ICommand DelCom { get; set; }
        public CustomerViewModel()
            AddCom = new DelegateCommand(Add);
            DelCom = new DelegateCommand(Del);
        private void Add(object o)
            Customers.Add(new Customer(){Id=Customers.Count,
                    Name="Customer "+ Customers.Count.ToString()} );
            OnPropertyChanged(() => this.Customers);
        private void Del(object o)
            Customers.Remove((Customer) o);
            OnPropertyChanged(() => this.Customers);
        public ObservableCollection<Customer> Customers
            get => this._customers;
                if (this._customers == value) return;
                this._customers = value;
                this.OnPropertyChanged(() => this.Customers);

    and XAML

    <Window x:Class="CustomListBoxDragDropBehavior.MainWindow"
            xmlns:mc="" mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525" >
            <telerik:RadContextMenu x:Key="Menu">
                <telerik:RadMenuItem Header="Add customer" Command="{Binding ??? }" />
                <telerik:RadMenuItem Header="Remove customer" Command="{Binding ???}"
                       CommandParameter=" ??? "/>/>
            <Style x:Key="ListBoxItem" TargetType="telerik:RadListBoxItem">
                <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource Menu}"/>
        <Grid >
            <telerik:RadListBox ItemContainerStyle="{StaticResource ListBoxItem}"
                                ItemsSource="{Binding Customers, Mode=TwoWay}"

    I tried few combination like 

    <telerik:RadMenuItem Header="Item" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.AddCom }" />

    with no success. Also I need to pass current customer to a DelCom as parameter.

    Please advise.


  2. Bill
    Bill avatar
    5 posts
    Member since:
    Dec 2019

    Posted 23 Jan in reply to Bill Link to this post

    I found if in my previous example change RadContextMenu to standard ContextMenu everything starts work perfectly, including command binding and command parameters passing.

    <ContextMenu x:Key="Menu">
        <MenuItem Header="Item" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.AddCom }" />
       <MenuItem Header="Item2" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.DelCom }"
              CommandParameter="{Binding }"/>
    <Style x:Key="ListBoxItem" TargetType="telerik:RadListBoxItem">
       <Setter Property="ContextMenu.ContextMenu" Value="{StaticResource Menu}"/>


    But when I substituted ContextMenu to RadContextMenu it all bindings looked broken. Context menu appears but  not call the commands.

    Is it a bug in a RadContextMenu or it should have different bindings format?


  3. Vladimir Stoyanov
    Vladimir Stoyanov avatar
    575 posts

    Posted 27 Jan Link to this post

    Hello Bill,

    Thank you for the provided code snippets.

    Can you check out the First Look RadContextMenu example from our demos? It demonstrates how to set the RadContextMenu on a ListBox, bind to commands from the viewmodel and also pass in the clicked item as the CommandParameter. 

    I hope you find this helpful.

    Vladimir Stoyanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top