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:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
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.