开发者

How can I execute a command binding on MouseEnter of a StackPanel in WPF?

开发者 https://www.devze.com 2023-01-05 11:49 出处:网络
I\'m using MVVM. <ItemsControl ItemsSource=\"{Binding AllIcons}\" Tag=\"{Binding}\"> <ItemsControl.ItemTemplate>

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?


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

}


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消