Monday, October 8, 2012

silverlight commanding event callback object

silverlight commanding event callback object

I am passing the button to my MouseEventCommand class.

My Command class is here

    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();     } } 

My silverlight code is here

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

and my Modev view is like this

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 ????????????????       } } 

My problem is my obj object is coming Null.

I break point in MouseEnterCommand class and I saw the button is coming here and ExecuteCommand is working well. But in model view, the obj is coming null.

Answers & Comments...

Answer: 1

Can you check ExecuteCommand method invoked inside OnMouseEnter eventhandler. You are not passing sender object any further from that place.

Verify whether sender object should be passed as input parameter to ExecuteCommand()

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();     } } 
by : Rakesh Gunijanhttp://stackoverflow.com/users/185550




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog