简体   繁体   中英

Why is this Command Dependency Set Value Property not triggered? (DevExpress.AgMenuItem)

I am using the free version of the DevExpress Silverlight Menu (AgMenu 8.4). Sadly the MenuItems of this menu have no "Command" and "CommandParameter" properties.

I decided to inherit from the MenuItem class and implement two DependencyProperties, "Command" and "CommandProperty".

The code for this looks like this:

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
    }



}

Now I am using this two DPs in my XAML. This looks like this for one 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>

When I am testing my silverlight application, I assume that both, the "Command" and "CommandParameter" set value properties are called, and the values are set to _Command and _CommandParameter, but only the CommandParameter set value is called.

Strangely, both static procedures "OnCommandChanged" and "OnCommandParameterChanged" are called. While debugging, I can see, both expected objects (ICommand and CommandParameter) are arriving in this two procedures.

So my question is:

What am I doing wrong, that the ICommand Object is not set in the "Set ICommand" property?

Thank you.

This case is solved. What I needed to do, is not using DependencyProperties, but attached DependencyProperties.

The MenuItem of the DevExpress Silverlight menu now accepts Command and CommandParameter objects. The Command is triggered, when the LeftMouseButtonUp event is fired. The following is the working code. The XAML stays the same (see above). You just need to make a silverlight user control and inherit from DevExpress.AgMenu.AgMenuItem. You then use this as MenuItem, instead of the original.

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


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM