我需要添加一个 MouseBinding
至 InputBindings
的 ListBoxItems
. 我知道如何在xaml中。
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border x:Name="MyBorder" ...>
<Border.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="{Binding SomeCommand}"/>
</Border.InputBindings>
</Border>
</Grid>
...
</ControlTemplate>
但我需要知道如何在代码后台写出上述代码。有什么办法可以做到这一点吗?
我想这就是你要找的。非常好的教程。MSDNhttp:/msdn.microsoft.comen-uslibrarysystem.windows.input.commandbinding.aspx。
我知道这不是答案,但如果有人找到了,可以参考一下。https:/stackoverflow.coma909198913179254。
就目前而言,使用 FrameworkElementFactory
微软不推荐使用。使用 XAML
而不是。
一个例子:
<Image.InputBindings>
<MouseBinding
MouseAction="LeftClick"
Command="{Binding ClearCommand, RelativeSource={RelativeSource TemplatedParent}}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</Image.InputBindings>