如果想要给TextBloc添加鼠标双击事件,有一个坑。
首先假设我们定义如下的Label(注意不是TextBlock)
<Label Grid.Column="1" Content="{Binding LabelContent}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="MouseDoubleClick">
<b:InvokeCommandAction Command="{Binding ClickCommand}" />
</b:EventTrigger>
</b:Interaction.Triggers>
</Label>
然后定义命令:
public RelayCommand ClickCommand { get; set; }
// somewhere
ClickCommand = new RelayCommand(() => MessageBox.Show("111"));
这样子,是能够顺利触发双击命令的。
但是,如果是TextBlock采用类似的定义的话,代码并不会报错,命令绑定却也不生效:
<TextBlock Text="{Binding Value}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="MouseDoubleClick">
<b:InvokeCommandAction Command="{Binding DoubleClickItemCommand}"/>
</b:EventTrigger>
</b:Interaction.Triggers>
</TextBlock>
这个挺奇怪的。
但不要气馁,不过还有其他的方式能够实现给TextBlock添加上双击事件:
<TextBlock Text="{Binding Value}">
<TextBlock.InputBindings>
<MouseBinding
Command="{Binding DoubleClickItemCommand}"
MouseAction="LeftDoubleClick" />
</TextBlock.InputBindings>
</TextBlock>
两个方式区别还是蛮大,记录下来分享一下。
参考链接:博客园文章