c# - 在MVVM中处理MouseEnter over命令

标签 c# wpf xaml mvvm command

我有一个 Canvas ,其中的ItemControl用自定义UserControlViewModels填充在ViewModel中。我想给每个UserControl一个MouseEnter命令或其他东西。

我的ViewModel的XAML如下:

<Canvas x:Name="gameFieldCanvas" Width="{Binding CanvasWidth}" Height="{Binding CanvasHeight}">
   <ItemsControl ItemsSource="{Binding GameFieldContent}">
      <ItemsControl.ItemTemplate>
         <DataTemplate DataType="{x:Type local:GameFieldTileViewModel}">
             <Canvas>
                <local:GameFieldTileUserControl X="{Binding TileX}" Y="{Binding TileY}">
                   <local:GameFieldTileUserControl.InputBindings>
                      <MouseBinding MouseAction="LeftClick"
                                                  Command="{Binding DataContext.OnGameFieldTileLeftClicked, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                  CommandParameter="{Binding}"/>
                      <MouseBinding MouseAction="RightClick" Command="{Binding DataContext.OnGameFieldTileRightClicked, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                  CommandParameter="{Binding}"/>
                   </local:GameFieldTileUserControl.InputBindings>
                </local:GameFieldTileUserControl>
             </Canvas>
         </DataTemplate>
      </ItemsControl.ItemTemplate>
   </ItemsControl>
</Canvas>

就像您看到的那样,我已经有Command for Left-/RightClick可以正常工作。但是没有MouseEnterEvent的InputBindings。

我已经读过关于Blend SDK的信息,您可以在其中编写EventTriggers或类似的东西,但是使用开发板工具不是吗?

我只想实现当输入UserControl时获取它的UserControlViewModel。

最佳答案

您能否详细说明最终要完成的工作?根据您的情况,我可能会为您提供更好的答案,但是您可以并且我有很多次覆盖现有控件并添加了所需的命令属性。

这是一个具有鼠标移动命令的自定义用户控件。

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPF_Question_Answer_App
{
    public partial class MouseMoveCommandUserControl : UserControl
    {
        public MouseMoveCommandUserControl()
        {
            InitializeComponent();
            MouseMove += (s, e) =>
            {
                if (MouseMoveCommand?.CanExecute(MouseMoveCommandParameter) ?? false)
                    MouseMoveCommand.Execute(MouseMoveCommandParameter);
            };
        }

        public ICommand MouseMoveCommand
        {
            get => (ICommand)GetValue(MouseMoveCommandProperty);
            set => SetValue(MouseMoveCommandProperty, value);
        }

        public static readonly DependencyProperty MouseMoveCommandProperty =
            DependencyProperty.Register(nameof(MouseMoveCommand), typeof(ICommand), typeof(MouseMoveCommandUserControl), new PropertyMetadata(null));

        public object MouseMoveCommandParameter
        {
            get => GetValue(MouseMoveCommandParameterProperty);
            set => SetValue(MouseMoveCommandParameterProperty, value);
        }

        public static readonly DependencyProperty MouseMoveCommandParameterProperty =
            DependencyProperty.Register(nameof(MouseMoveCommandParameter), typeof(object), typeof(MouseMoveCommandUserControl), new PropertyMetadata(null));
    }
}

这是显而易见的。
<Window x:Class="WPF_Question_Answer_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Question_Answer_App"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MouseMoveCommandUserControl MouseMoveCommand="{Binding SomeMouseMoveCommand}"
                                           MouseMoveCommandParameter="{Binding SomeMouseMoveCommandParameter}"/>
    </Grid>
</Window>

希望这会有所帮助...如果没有,我将删除答案。

关于c# - 在MVVM中处理MouseEnter over命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583941/

相关文章:

c# - 更新 Azure 服务总线队列上已有的代理消息

c# - Keybd_event 下划线提供错误的输出

wpf - VS2019更新18.2缺少 "WPF App .NET Core"项目模板

wpf - 如何将子文件夹中定义的 ResourceDictionary 添加到 App.xaml?

WPF:Windows 7 与 Windows 10 外观

c# - ListView SelectedItem 的属性和 TextBox 绑定(bind)发生 ArgumentException

c# - Asp.Net MVC - 无法开始监视文件更改

c# - 如何更改 WPF 复选框勾号的颜色?

c# - 自定义项目类型 Visual Studio 中的自定义 XAML 设计器

c# - WPF 等待从 UI 线程切换