WPF MVVM 鼠标双击事件

<local:MonthItem.InputBindings>
     <MouseBinding Command="{Binding DataContext.MonthItemDoubleClick,ElementName=MonthlySchedule}" MouseAction="LeftDoubleClick" CommandParameter="{Binding ElementName=Item}"/>
     </local:MonthItem.InputBindings>

这里面 local:monthitem 是我的自义定控件 所以可以替换成任何控件 主要是 MouseBinding代码  ElementName 指定的是窗体的名字 

全部代码

    <ItemsControl  Margin="0" ItemsSource="{Binding Warplist}"   VerticalAlignment="Bottom" Grid.Row="1"  >

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel  ItemHeight="136" ItemWidth="215" />
                    </ItemsPanelTemplate>

                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <local:MonthItem x:Name="Item" No="{Binding NO}" MonthScheduleModel="{Binding MonthScheduleModel}"  IsTrue="{Binding IsTrue}">
                            <local:MonthItem.InputBindings>
                                <MouseBinding Command="{Binding DataContext.MonthItemDoubleClick,ElementName=MonthlySchedule}" MouseAction="LeftDoubleClick" CommandParameter="{Binding ElementName=Item}"/>
                            </local:MonthItem.InputBindings>
                        </local:MonthItem>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>

WPF MVVM中实现鼠标移动事件,可以使用以下步骤: 1.在ViewModel中定义一个命令,例如: ``` public ICommand MouseMoveCommand { get; set; } public ViewModel() { MouseMoveCommand = new RelayCommand<MouseEventArgs>(MouseMove); } private void MouseMove(MouseEventArgs e) { // 处理鼠标移动事件 } ``` 在这个例子中,我们定义了一个MouseMoveCommand命令,它将鼠标移动事件处理程序绑定到ViewModel中的MouseMove方法。我们使用RelayCommand类来实现这个命令,这个类可以将任何方法转换为ICommand接口。 2.在XAML中绑定MouseMove事件到ViewModel中定义的MouseMoveCommand命令。例如: ``` <Canvas MouseMove="{Binding MouseMoveCommand}"> <!-- 在Canvas中添加其他控件 --> </Canvas> ``` 在这个例子中,我们将Canvas的MouseMove事件绑定到ViewModel中的MouseMoveCommand命令。这将自动调用ViewModel中的MouseMove方法来处理鼠标移动事件。 3.在ViewModel中实现MouseMove方法。例如: ``` private void MouseMove(MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { // 处理鼠标拖动事件 } else { // 处理鼠标移动事件 } } ``` 在这个例子中,我们检查鼠标左键是否按下。如果是,我们处理鼠标拖动事件。否则,我们处理鼠标移动事件。 注意,在MVVM模式中,我们将所有用户交互操作转移到ViewModel中处理。这样可以使应用程序更容易维护和测试,因为View不再包含任何业务逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慧哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值