Wpf silverlight命令事件回调对象

Wpf silverlight命令事件回调对象,wpf,silverlight,binding,command,prism,Wpf,Silverlight,Binding,Command,Prism,我正在将按钮传递给我的MouseEventCommand类 我的指挥班在这里 public static class MouseEnterCommand { public static ICommand GetCommand(Button button) { return button.GetValue(CommandProperty) as ICommand; } public static void SetCommand(Button

我正在将按钮传递给我的MouseEventCommand类

我的指挥班在这里

    public static class MouseEnterCommand
{
    public static ICommand GetCommand(Button button)
    {
        return button.GetValue(CommandProperty) as ICommand;
    }

    public static void SetCommand(Button button, ICommand command)
    {
        button.SetValue(CommandProperty, command);
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached(
            "Command",
            typeof (ICommand),
            typeof (MouseEnterCommand),
            new PropertyMetadata(OnSetCommandCallback));


    public static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        Button button = dependencyObject as Button;

        if (button != null)
        {
            MouseEnterChangedBehavior behavior = GetOrCreateObject(button);
            behavior.Command = eventArgs.NewValue as ICommand;
        }

    }

    private static MouseEnterChangedBehavior GetOrCreateObject(Button button)
    {
        MouseEnterChangedBehavior behavior = button.GetValue(MouseEnterCommandBehaviorProperty) as MouseEnterChangedBehavior;
        if (behavior == null)
        {
            behavior = new MouseEnterChangedBehavior(button);
            button.SetValue(MouseEnterCommandBehaviorProperty, behavior);
        }
        return behavior;
    }

    private static readonly DependencyProperty MouseEnterCommandBehaviorProperty =
        DependencyProperty.RegisterAttached(
            "MouseEnterCommandBehavior",
            typeof (object),
            typeof (MouseEnterCommand),
            null);

    public static ICommand GetCommandParameter(Button button)
    {
        return button.GetValue(CommandParameterProperty) as ICommand;
    }

    public static void SetCommandParameter(Button button, object parameter)
    {
        button.SetValue(CommandParameterProperty, parameter);
    }

    public static readonly DependencyProperty CommandParameterProperty =
      DependencyProperty.RegisterAttached(
        "CommandParameter",
        typeof(object),
        typeof(MouseEnterCommand),
        new PropertyMetadata(OnSetCommandParameterCallback));

    public static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        Button button = dependencyObject as Button;

        if (button != null)
        {
            MouseEnterChangedBehavior behavior = GetOrCreateObject(button);
            behavior.Command = eventArgs.NewValue as ICommand;
        }

    }
}

public class MouseEnterChangedBehavior:CommandBehaviorBase<Button>
{
    public MouseEnterChangedBehavior(Button targetObject) 
        : base(targetObject)
    {
        targetObject.MouseEnter += OnMouseEnter;
    }

    private void OnMouseEnter(object sender, MouseEventArgs e)
    {
        ExecuteCommand();
    }
}
公共静态类MouseEnterCommand
{
公共静态ICommand GetCommand(按钮)
{
将button.GetValue(CommandProperty)作为ICommand返回;
}
公共静态void SetCommand(按钮按钮、ICommand命令)
{
设置值(CommandProperty,command);
}
公共静态只读DependencyProperty CommandProperty=
DependencyProperty.RegisterAttached(
“命令”,
类型(ICommand),
类型(MouseEnterCommand),
新属性元数据(OnSetCommandCallback));
公共静态void OnSetCommandCallback(DependencyObject DependencyObject,DependencyPropertyChangedEventArgs eventArgs)
{
Button Button=作为按钮的dependencyObject;
如果(按钮!=null)
{
MouseEnterChangedBehavior行为=GetOrCreateObject(按钮);
behavior.Command=eventArgs.NewValue作为ICommand;
}
}
专用静态鼠标InterChangedBehavior GetOrCreateObject(按钮)
{
MouseEnterChangedBehavior行为=button.GetValue(MouseEnterCommandBehaviorProperty)作为MouseEnterChangedBehavior;
if(行为==null)
{
行为=新鼠标更改行为(按钮);
设置值(MouseEnterCommandBehaviorProperty,behavior);
}
回归行为;
}
私有静态只读从属属性MouseEnterCommandBehaviorProperty=
DependencyProperty.RegisterAttached(
“MouseEnterCommandBehavior”,
类型(对象),
类型(MouseEnterCommand),
无效);
公共静态ICommand GetCommandParameter(按钮)
{
返回按钮.GetValue(CommandParameterProperty)作为ICommand;
}
公共静态void SetCommandParameter(按钮、对象参数)
{
设置值(CommandParameterProperty,参数);
}
public static readonly dependencProperty命令参数解释属性=
DependencyProperty.RegisterAttached(
“命令参数”,
类型(对象),
类型(MouseEnterCommand),
新属性元数据(OnSetCommandParameterCallback));
公共静态void OnSetCommandParameterCallback(DependencyObject DependencyObject,DependencyPropertyChangedEventArgs)
{
Button Button=作为按钮的dependencyObject;
如果(按钮!=null)
{
MouseEnterChangedBehavior行为=GetOrCreateObject(按钮);
behavior.Command=eventArgs.NewValue作为ICommand;
}
}
}
公共类MouseEnterChangedBehavior:CommandBehaviorBase
{
公共鼠标InterChangedBehavior(按钮目标对象)
:基本(目标对象)
{
targetObject.MouseEnter+=OnMouseEnter;
}
MouseEventArgs e上的私有void(对象发送方)
{
ExecuteCommand();
}
}
我的silverlight代码在这里

        <Button Width="250"
            Height="60"
            Content="MyButton with MouseEnter"
            commands:MouseEnterCommand.Command="{Binding MouseEnterCommand}"
            commands:MouseEnterCommand.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
            />

我的Modev视图是这样的

public class MouseEnterViewModel
{
    private ICommand mouseEnterCommand;

    public ICommand MouseEnterCommand
    {
        get
        {
            if(mouseEnterCommand==null)
            {
                mouseEnterCommand =new DelegateCommand<object>(OnButtonMouseEnter);
            }
            return mouseEnterCommand;
        }
    }

    public void OnButtonMouseEnter(object obj)
    {
        // !!!!!!!  obj is coming null ????????????????


    }
}
public类MouseEnterViewModel
{
专用ICommand mouseEnterCommand;
公共ICommand MouseEnterCommand
{
得到
{
if(mouseEnterCommand==null)
{
mouseEnterCommand=新的DelegateCommand(onButtonMouseCenter);
}
返回mouseenter命令;
}
}
public void onButtonMouseCenter(对象对象对象)
{
//!!!!!!!!!obj将为空????????????????
}
}
我的问题是我的obj对象将为空


我在MouseEnterCommand类中遇到了一个转折点,我看到按钮出现在这里,ExecuteCommand运行良好。但是在模型视图中,obj为空。

您能检查一下在OnMouseCenter eventhandler中调用的ExecuteCommand方法吗。您不能将发送者对象传递到离该位置更远的地方

验证是否应将发件人对象作为输入参数传递给ExecuteCommand()

public类MouseEnterChangedBehavior:CommandBehaviorBase
{
公共鼠标InterChangedBehavior(按钮目标对象)
:基本(目标对象)
{
targetObject.MouseEnter+=OnMouseEnter;
}
MouseEventArgs e上的私有void(对象发送方)
{//检查是否可以将sender对象作为输入传递到此方法
ExecuteCommand();
}
}

是的,我查过了。发送方对象作为按钮出现。你可以从这里看到。此外,此执行命令将转到MouseEnterViewModel的onButtonMouseCenter(对象obj)方法。但obj参数即将为空。
public class MouseEnterChangedBehavior:CommandBehaviorBase<Button>
{
    public MouseEnterChangedBehavior(Button targetObject) 
        : base(targetObject)
    {
        targetObject.MouseEnter += OnMouseEnter;
    }

    private void OnMouseEnter(object sender, MouseEventArgs e)
    {  // check if you can pass sender object as input to this method
        ExecuteCommand();
    }
}