How can I bind a command in MouseEnter from a StackPanel in WPF?

I am 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>

It looks good. If I put a button on the stack panel using this command:

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

I can capture the team. However, I want to bind the command when the mouse enters the stack panel, and not when I click the button.

Any idea?

+3
source share
3 answers

Wrong, input bindings do not solve the problem. For this you can use the attached properties:

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

}
+2
source

First you need to declare the behavior for mouse input. This basically translates the event into a command in the 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);
      }
    }
  }

. : , . , .

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

    </StackPanel>
</Grid>
+2
0
source

Source: https://habr.com/ru/post/1752520/


All Articles