如何在WPF用户控件中的整个网格LeftClick上绑定命令

问题描述

我想在用户控件内实现Grid Left Click。

我的WPF用户控件

  <Grid>
       <Border Grid.Row="0" CornerRadius="7" >
            <Border.Background>
                <SolidColorBrush Color="#ffffff" Opacity="0.08"></SolidColorBrush>
            </Border.Background>
            <Grid>
                <Image Source="/Assets/Images/Icon/ic-add.png" Width="70" Height="70"/>
            </Grid>
        </Border>
 </Grid>

这是我的窗户

<Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <usercontrols:CreateNewProfile Width="200" Height="235" Margin="40,40,0"> 
        </usercontrols:CreateNewProfile>           
    </<StackPanel>
</Grid>

一个名为CreateNewProfile的命令

非常直截了当的问题,如何在用户控件的左键单击上绑定命令?

解决方法

您可以在UserControl内使用InputBindings:

    <Grid.InputBindings>
        <MouseBinding MouseAction="LeftClick" Command="{Binding CreateNewProfile}"/>
    </Grid.InputBindings>