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 @ 2021-06-29 11:07  xhubobo  阅读(401)  评论(0编辑  收藏  举报