首页 > 解决方案 > 使用 wpf 中的 InputBindings 使整个用户控件可点击

问题描述

在我的用户控件中,我在画布内绘制了一些路径

为了打开这个 UserControl 的控制面板,我使用了 InputBindings,如下所示:

<UserControl.InputBindings>
    <MouseBinding Gesture="LeftClick" Command="{Binding OpenControlPanelCommand}"/>
</UserControl.InputBindings>

这仅在我单击已绘制的路径时才有效。实际上,我希望所有 UserControl 的表面都可以点击,而不仅仅是绘制的部分

我设置了用户控件的背景,但它不起作用

我怎样才能改变这种行为?

标签: wpfbindinguser-controls

解决方案


这对我有用:

<UserControl …
         Background="Transparent"
         >
    <UserControl.InputBindings>
       <MouseBinding Gesture="LeftClick" Command="{Binding OpenControlPanelCommand}"/>
    </UserControl.InputBindings>
    <Canvas>
        <Path Data="M0,0L25.194,16 0,32z" Stretch="Fill"
              Height="100"
              Width="100"
              Fill="Black"/>
    </Canvas>
</UserControl>

我把它放在一个窗口中:

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <local:UserControl1/>
</Grid>

vm的相关部分:

using …
using GalaSoft.MvvmLight.CommandWpf;
namespace wpf_99
{
    public class MainWindowViewModel : INotifyPropertyChanged
   {
    public ICommand OpenControlPanelCommand { get; set; } = new RelayCommand(() => MessageBox.Show("Hello World"));

您的用户控件是否填充了您期望的区域?

您的命令绑定正确吗?

上面有什么东西可以抓住点击吗?


推荐阅读