Prism框架下对Grid鼠标点击做命令绑定

使用Interaction.Triggers可以对依赖属性进行命令响应,适合在Prism框架中使用ViewModel进行命令绑定。

使用Interaction需要引用System.Windows.Interactivity。

参考:WPF how to bind mousedown (command/action) to label

1、View

复制代码
<Grid>
    <Border Style="{StaticResource BorderRegion}">
        <Grid>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    <i:InvokeCommandAction Command="{Binding SelectGridCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Grid>
    </Border>
</Grid>
复制代码

2、ViewModel

复制代码
public class WindowViewModel : BindableBase
{
    public DelegateCommand SelectGridCommand { get; }

    public WindowViewModel()
    {
        SelectGridCommand = new DelegateCommand(SelectGrid, CanExecute);
    }
    
    private static bool CanExecute()
    {
        return true;
    }

    private void SelectGrid()
    {
    }
}
复制代码
posted @   xhubobo  阅读(421)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示