使用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()
    {
    }
}

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-01-06
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
相关资源
相似解决方案