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

    Hi, 

    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;
            set
            {
                if (this._customers == value) return;
                this._customers = value;
                this.OnPropertyChanged(() => this.Customers);
            }
        }
    }

    and XAML

    <Window x:Class="CustomListBoxDragDropBehavior.MainWindow"
            xmlns:local="clr-namespace:CustomListBoxDragDropBehavior"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525" >
        <d:Window.DataContext>
            <local:CustomerViewModel/>
        </d:Window.DataContext>
        <Window.Resources>
            <telerik:RadContextMenu x:Key="Menu">
                <telerik:RadMenuItem Header="Add customer" Command="{Binding ??? }" />
                <telerik:RadMenuItem Header="Remove customer" Command="{Binding ???}"
                       CommandParameter=" ??? "/>/>
            </telerik:RadContextMenu>
            <Style x:Key="ListBoxItem" TargetType="telerik:RadListBoxItem">
                <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource Menu}"/>
            </Style>
        </Window.Resources>
     
        <Grid >
            <telerik:RadListBox ItemContainerStyle="{StaticResource ListBoxItem}"
                                ItemsSource="{Binding Customers, Mode=TwoWay}"
                                DisplayMemberPath="Name"
                                >
            </telerik:RadListBox>
        </Grid>
    </Window>

    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 }"/>
    </ContextMenu>
    <Style x:Key="ListBoxItem" TargetType="telerik:RadListBoxItem">
       <Setter Property="ContextMenu.ContextMenu" Value="{StaticResource Menu}"/>
    </Style>

     

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

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