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

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

2 Answers 503 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 22 Jan 2020, 10:20 PM

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 Answers, 1 is accepted

Sort by
0
Bill
Top achievements
Rank 1
answered on 23 Jan 2020, 04:11 PM

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?

 

0
Vladimir Stoyanov
Telerik team
answered on 27 Jan 2020, 02:00 PM

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.
Tags
ListBox
Asked by
Bill
Top achievements
Rank 1
Answers by
Bill
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or