未触发 MouseBinding 命令
MouseBinding Command is not fired
这是我的 XAML 代码:
<Grid>
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />
</Grid.InputBindings>
</Grid>
和我的代码隐藏:
private RelayCommand _MouseDoubleClick;
public ICommand MouseDblClick
{
get
{
if (_MouseDoubleClick == null)
{
_MouseDoubleClick = new RelayCommand(param => Clicked());
}
return _MouseDoubleClick;
}
}
private void Clicked()
{
MessageBox.Show("Works");
}
我希望在双击网格后显示消息框。但是没有任何反应。哪里错了?
另一种解决方案的建议:
我不是命令的粉丝,如果它可以帮助你,我有另一个基于触发事件的解决方案:
1) 添加对 System.Windows.Interactivity 和 Microsoft.Expression.Interactions DLL 的引用(位于 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4。 0\Libraries)
2) 将此添加到您的 XAML 文件中:
xmlns:l="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
3) 在您的网格中添加:
<Grid...>
<l:Interaction.Triggers>
<l:EventTrigger EventName="DoubleClick">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="Clicked" />
</l:EventTrigger>
</l:Interaction.Triggers>
...
</Grid>
4) 制定你的方法 "Clicked()" Public
希望对您有所帮助。
正如 icebat 在他的评论中提到的那样。尝试用一些 Brush
设置 Grid
的 BG
<Grid Background="Green">
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />
</Grid.InputBindings>
</Grid>
默认情况下,Grid
将具有 Background="{x:Null}"
,如前所述 here
不可点击
此外,请确保您已将 VM 对象传递给 View
的 DataContext
像这样this.DataContext = new ViewModel();
这是我的 XAML 代码:
<Grid>
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />
</Grid.InputBindings>
</Grid>
和我的代码隐藏:
private RelayCommand _MouseDoubleClick;
public ICommand MouseDblClick
{
get
{
if (_MouseDoubleClick == null)
{
_MouseDoubleClick = new RelayCommand(param => Clicked());
}
return _MouseDoubleClick;
}
}
private void Clicked()
{
MessageBox.Show("Works");
}
我希望在双击网格后显示消息框。但是没有任何反应。哪里错了?
另一种解决方案的建议:
我不是命令的粉丝,如果它可以帮助你,我有另一个基于触发事件的解决方案:
1) 添加对 System.Windows.Interactivity 和 Microsoft.Expression.Interactions DLL 的引用(位于 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4。 0\Libraries)
2) 将此添加到您的 XAML 文件中:
xmlns:l="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
3) 在您的网格中添加:
<Grid...>
<l:Interaction.Triggers>
<l:EventTrigger EventName="DoubleClick">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="Clicked" />
</l:EventTrigger>
</l:Interaction.Triggers>
...
</Grid>
4) 制定你的方法 "Clicked()" Public
希望对您有所帮助。
正如 icebat 在他的评论中提到的那样。尝试用一些 Brush
Grid
的 BG
<Grid Background="Green">
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding MouseDblClick}" />
</Grid.InputBindings>
</Grid>
默认情况下,Grid
将具有 Background="{x:Null}"
,如前所述 here
此外,请确保您已将 VM 对象传递给 View
DataContext
像这样this.DataContext = new ViewModel();