wpf - 使用 wpf 中的 InputBindings 使整个用户控件可点击
问题描述
在我的用户控件中,我在画布内绘制了一些路径
为了打开这个 UserControl 的控制面板,我使用了 InputBindings,如下所示:
<UserControl.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding OpenControlPanelCommand}"/>
</UserControl.InputBindings>
这仅在我单击已绘制的路径时才有效。实际上,我希望所有 UserControl 的表面都可以点击,而不仅仅是绘制的部分
我设置了用户控件的背景,但它不起作用
我怎样才能改变这种行为?
解决方案
这对我有用:
<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"));
您的用户控件是否填充了您期望的区域?
您的命令绑定正确吗?
上面有什么东西可以抓住点击吗?
推荐阅读
- keras - 如何在 keras 中强制执行特征正交性
- java - 在 javafx 中,即使删除了第二个默认按钮,第一个默认按钮也不起作用
- jquery - 如何设置表格的滚动条正确位置?
- git - 具有密码保护的本地 git 存储库
- javascript - 有什么方法可以在不使用警报的情况下打印 ajax 成功响应
- c - 进入 switch case 将每个变量重置为零和一些提示
- java - 在 ubuntu 18.04 中通过 Eclipse 与 mySql(mysql 5.7.22)进行 JDBC 连接时出错
- javascript - 通过其包含的文本从 html 中删除项目
- rabbitmq - 如何在nodejs中实现rabbitmq
- r - 如何在平衡数据集上应用 svm