Hi,
is it possible to bind a XamMenuItem click to a command object?
Regards
Marco
I am attaching a sample project for your review.
This sample binds the XamMenuItem's click to a command.
Sincerely, Matt DSE
Great, thank you.
Also it is possible to use Behaviors. Please see the implemented XamMenuItemCommandBehavior behavior below:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Infragistics.Controls.Menus;
namespace GoIT.Dashboard.Infrastructure.Behaviors
{
public class XamMenuItemCommandBehavior:Behavior<XamMenuItem>
/// <summary>
/// The <see cref="Command" /> dependency property's name.
/// </summary>
public const string CommandPropertyName = "Command";
/// Gets or sets the value of the <see cref="Command" />
/// property. This is a dependency property.
public ICommand Command
get
return (ICommand)GetValue(CommandProperty);
}
set
SetValue(CommandProperty, value);
/// Identifies the <see cref="Command" /> dependency property.
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(CommandPropertyName, typeof(ICommand), typeof(XamMenuItemCommandBehavior), null);
/// The <see cref="CommandParamenter" /> dependency property's name.
public const string CommandParamenterPropertyName = "CommandParamenter";
/// Gets or sets the value of the <see cref="CommandParamenter" />
public object CommandParamenter
return (object)GetValue(CommandParamenterProperty);
SetValue(CommandParamenterProperty, value);
/// Identifies the <see cref="CommandParamenter" /> dependency property.
public static readonly DependencyProperty CommandParamenterProperty = DependencyProperty.Register(CommandParamenterPropertyName, typeof(object), typeof(XamMenuItemCommandBehavior), null);
protected override void OnAttached()
base.OnAttached();
AssociatedObject.Click += AssociatedObject_Click;
private void AssociatedObject_Click(object sender, EventArgs e)
if (Command != null && Command.CanExecute(CommandParamenter))
Command.Execute(null);
protected override void OnDetaching()
AssociatedObject.Click -= AssociatedObject_Click;
base.OnDetaching();