C# 为什么未触发此命令依赖项集值属性?(DevExpress.AgMenuItem)
我正在使用免费版本的DevExpress Silverlight菜单(AgMenu 8.4)。遗憾的是,此菜单的菜单项没有“Command”和“CommandParameter”属性 我决定继承MenuItem类并实现两个DependencyProperty,“Command”和“CommandProperty” 此操作的代码如下所示:C# 为什么未触发此命令依赖项集值属性?(DevExpress.AgMenuItem),c#,devexpress,dependency-properties,silverlight-5.0,menuitem,C#,Devexpress,Dependency Properties,Silverlight 5.0,Menuitem,我正在使用免费版本的DevExpress Silverlight菜单(AgMenu 8.4)。遗憾的是,此菜单的菜单项没有“Command”和“CommandParameter”属性 我决定继承MenuItem类并实现两个DependencyProperty,“Command”和“CommandProperty” 此操作的代码如下所示: public partial class MenuItem : DevExpress.AgMenu.AgMenuItem { public MenuIt
public partial class MenuItem : DevExpress.AgMenu.AgMenuItem
{
public MenuItem()
{
InitializeComponent();
}
private Object _CommandParameter = null;
public Object CommandParameter
{
get { return _CommandParameter; }
set { _CommandParameter = value; } //This one is triggered. This is ok.
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(Object), typeof(Gui.CustomControls.MenuItem), new PropertyMetadata(OnCommandParameterChanged));
private static void OnCommandParameterChanged(object sender, DependencyPropertyChangedEventArgs args)
{
//CommandParameter Object is arriving here. That is ok.
}
private ICommand _Command = null;
public ICommand Command
{
get { return _Command; }
set
{
//HERE is the problem.
//This one is NOT triggered. I dont' know why....?
_Command = value;
}
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(Gui.CustomControls.MenuItem), new PropertyMetadata(OnCommandChanged));
private static void OnCommandChanged(object sender, DependencyPropertyChangedEventArgs args)
{
//ICommand Object is arriving here. That is also ok.
//I don't understand, why the ICommand Object is not arriving in the set value prop
}
}
现在我在XAML中使用这两个DPs。对于一个菜单项,看起来是这样的:
<cc:MenuItem x:Name ="_mnuItemLogout"
DataContext ="{Binding Source={StaticResource ViewModel}}"
Header ="{Binding Source={StaticResource MenuProvider}, Path=GetSingleton.LogoutText, Mode=OneWay}"
IsEnabled ="{Binding Source={StaticResource MenuProvider}, Path=GetSingleton.LogoutEnabled, Mode=OneWay}"
Command ="{Binding Source={StaticResource ViewModel}, Path=Command_FormOpen}"
CommandParameter ="{gui:FormOpen e=Login}"
IsCheckable ="False"
>
</cc:MenuItem>
当我测试silverlight应用程序时,我假设同时调用了“Command”和“CommandParameter”设置值属性,并将值设置为_Command和_CommandParameter,但只调用CommandParameter设置值。
奇怪的是,静态过程“OnCommandChanged”和“OnCommandParameterChanged”都被调用。在调试时,我可以看到,两个预期的对象(ICommand和CommandParameter)都到达了这两个过程中
所以我的问题是:
我做错了什么,没有在“set ICommand”属性中设置ICommand对象?
谢谢。这个案子已经解决。我需要做的是不是使用DependencyProperties,而是附加DependencyProperties。 DevExpress Silverlight菜单的菜单项现在接受Command和CommandParameter对象。触发LeftMouseButtonUp事件时,将触发该命令。以下是工作代码。XAML保持不变(见上文)。您只需创建silverlight用户控件并从DevExpress.AgMenu.AgMenuItem继承即可。然后将其用作菜单项,而不是原始菜单项
using System;
using System.Windows;
using System.Windows.Input;
namespace Gui.CustomControls
{
public partial class MenuItem : DevExpress.AgMenu.AgMenuItem
{
public MenuItem()
{
InitializeComponent();
}
#region CommandParameter DependencyProperty
public static Object GetCommandParameter(DependencyObject obj)
{
return (Object)obj.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DependencyObject obj, Object value)
{
obj.SetValue(CommandParameterProperty, value);
}
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(Object), typeof(Gui.CustomControls.MenuItem), new PropertyMetadata(OnCommandParameterChanged) );
private static void OnCommandParameterChanged(object sender, DependencyPropertyChangedEventArgs args)
{
DependencyObject _DependencyObject = (DependencyObject)sender;
if ((args.NewValue != null) && (_DependencyObject != null))
{
MenuItem.SetCommandParameter(_DependencyObject, args.NewValue);
}
}
#endregion
#region Command
private static void OnCommandChanged(object sender, DependencyPropertyChangedEventArgs args)
{
DependencyObject _DependencyObject = (DependencyObject)sender;
ICommand _ICommand = (ICommand)args.NewValue;
if ((_ICommand != null) && (_DependencyObject != null))
{
SetCommand(_DependencyObject, _ICommand);
}
}
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
// Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(Gui.CustomControls.MenuItem), new PropertyMetadata(OnCommandChanged));
#endregion
#region LeftMouseButtonUp (Command Trigger)
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
ICommand _ICommand = MenuItem.GetCommand(this);
Object _CommandParameter = MenuItem.GetCommandParameter(this);
if (_ICommand != null)
{
_ICommand.Execute(_CommandParameter);
}
}
#endregion
}
}