views:

36

answers:

3

I'm using MVVM.

<ItemsControl ItemsSource="{Binding AllIcons}" Tag="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label HorizontalAlignment="Right">x</Label>
                <Image Source="{Binding Source}" Height="100" Width="100" />
                <Label HorizontalAlignment="Center" Content="{Binding Title}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

That looks fine. If I put a button in the stack panel using this command:

<Button Command="{Binding Path=DataContext.InvasionCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}"/>

I'm able to capture the command. However, I want to execute the command binding when the mouse enters the stack panel, not when I click a button.

Any idea?

A: 

You probably need to use InputBindings: http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx

STO
I tried playing around with inputbindings but all the mouse bindings require some kind of "action", which means a click. I wasn't able to locate any support for MouseEnter events or something like that.
hypoxide
+1  A: 

My wrong, input bindings does not solve the problem. You may use attached properties for this:

public static class MouseEnterCommandBinding
{
     public static readonly DependencyProperty MouseEnterCommandProperty = DependencyProperty.RegisterAttached(
  "MouseEnterCommand",
  typeof(ICommand),
  typeof(MouseEnterCommandBinding),
  new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);

public static void SetMouseEnterCommand(UIElement element, ICommand value)
{ 
   element.SetValue(MouseEnterCommandProperty, value);
   element.MouseEnter += (s,e) => 
   {
      var uiElement = s as UIElement;
      var command = GetMouseEnterCommand(uiElement); 
      if (command != null && command.CanExecute(uiElement.CommandParameter))
          command.Execute(uiElement.CommandParameter);
   }  
}
public static ICommand GetMouseEnterCommand(UIElement element)
{
    return element.GetValue(MouseEnterCommandProperty) as ICommand;
}

}
STO
Is there any way to do this with the MVVM architecture?
hypoxide
Sure, you may bind that dependency property to command from your view model, like:`xmlns:view="import your namespace with MouseEnterCommandBinding class"...<StackPanel view:MouseEnterCommandBinding.MouseEnterCommand={Binding YourCommand}> <StackPanel.DataContext> <model:YourModel /> </StackPanel.DataContext></StackPanel>`By the way, UIElement does not contain CommandParameter property until it implements ICommandSource interface, so you need to change SetMouseEnterCommand method to reflect this fact.
STO
A: 

First you need to declare a behavior for mouse enter. This basically translates the event into a command in your ViewModel.

  public static class MouseEnterBehavior
  {
     public static readonly DependencyProperty MouseEnterProperty =
        DependencyProperty.RegisterAttached("MouseEnter",
                                            typeof(ICommand),
                                            typeof(MouseEnterBehavior),
                                            new PropertyMetadata(null, MouseEnterChanged));

    public static ICommand GetMouseEnter(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(MouseEnterProperty);
    }

    public static void SetMouseEnter(DependencyObject obj, ICommand value)
    {
      obj.SetValue(MouseEnterProperty, value);
    }

    private static void MouseEnterChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
      UIElement uiElement = obj as UIElement;

      if (uiElement != null)
        uiElement.MouseEnter += new MouseEventHandler(uiElement_MouseEnter);
    }

    static void uiElement_MouseEnter(object sender, MouseEventArgs e)
    {      
      UIElement uiElement = sender as UIElement;
      if (uiElement != null)
      {
        ICommand command = GetMouseEnter(uiElement);
        command.Execute(uiElement);
      }
    }
  }

Then you just need to create that command in your view model and reference it in the view. The behaviors: namespace should just point to wherever you created that behavior. I use this pattern every time I need to translate an event into a command in a view model.

<Grid>
    <StackPanel behaviors:MouseEnterBehavior.MouseEnter="{Binding MouseEnteredCommand}"
                Height="150"
                Width="150"
                Background="Red">

    </StackPanel>
</Grid>
Shaun Bowe