如何在WPF中的StackPanel的MouseEnter上执行命令绑定?

时间:2010-06-30 18:49:23

标签: wpf vb.net mvvm

我正在使用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>

看起来很好。如果我使用此命令在堆栈面板中放置一个按钮:

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

我能够捕获命令。但是,我想在鼠标进入堆栈面板时执行命令绑定,而不是在单击按钮时执行命令绑定。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

我错了,输入绑定无法解决问题。您可以使用附加属性:

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

}

答案 1 :(得分:2)

首先,您需要声明鼠标输入的行为。这基本上将事件转换为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)