为什么不触发此命令依赖项集值属性? (DevExpress.AgMenuItem)

时间:2013-04-30 11:14:18

标签: c# devexpress dependency-properties silverlight-5.0 menuitem

我正在使用免费版的DevExpress Silverlight菜单(AgMenu 8.4)。遗憾的是,此菜单的MenuItems没有“Command”和“CommandParameter”属性。

我决定从MenuItem类继承并实现两个DependencyProperties,“Command”和“CommandProperty”。

此代码如下所示:

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中使用这两个DP。对于一个MenuItem,这看起来像这样:

<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)都在这两个程序中到达。

所以我的问题是:

我做错了什么,ICommand对象未在“设置ICommand”属性中设置?

谢谢。

1 个答案:

答案 0 :(得分:0)

此案例已经解决。我需要做的是使用DependencyProperties,但附加了DependencyProperties。

DevExpress Silverlight菜单的MenuItem现在接受Command和CommandParameter对象。触发LeftMouseButtonUp事件时触发命令。以下是工作代码。 XAML保持不变(见上文)。您只需要创建一个silverlight用户控件并从DevExpress.AgMenu.AgMenuItem继承。然后将其用作MenuItem,而不是原始。

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


}

}