Wpf 在ListViewItem中设置InputBinding(键盘绑定)
我想设置listViewitem的Inputbinding。。。它应该是键盘绑定而不是鼠标绑定 当用户选择一个项目并按下Wpf 在ListViewItem中设置InputBinding(键盘绑定),wpf,listview,mvvm,key-bindings,inputbinding,Wpf,Listview,Mvvm,Key Bindings,Inputbinding,我想设置listViewitem的Inputbinding。。。它应该是键盘绑定而不是鼠标绑定 当用户选择一个项目并按下Enter键时,我想在我的视图模型中执行一个函数 ListViewItem的样式 我的数据模板 我似乎找不到一个方法来做这件事 我在DataTemplate中附加了我的InputBinding,但在样式中没有任何效果 <KeyBinding Key="Enter" Command="{Binding Path=
Enter
键时,我想在我的视图模型中执行一个函数
ListViewItem的样式
我的数据模板
我似乎找不到一个方法来做这件事
我在DataTemplate中附加了我的InputBinding,但在样式中没有任何效果
<KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding>
您是否使用4.0之前的.Net版本?如果是这样,
KeyBinding.Command
和KeyBinding.CommandParameter
上的绑定将无法工作。为此,您必须使用CommandReference
API
否则,如果您使用的是.Net 4.0,则
KeyBinding
添加到ListView
的InputBindings
KeyBinding.CommandParameter
绑定到列表视图的SelectedItem
通过这种方式,命令将在按下参数时执行,该参数将是ListView的选定项(我认为您希望实现该项)我使用的是.Net 4.0客户端配置文件。。。。我甚至尝试使用Net 4.0键绑定。如果我在选择ListViewitem后按一个
选项卡
,该命令会起作用。但是如果我必须使用相同的键为ListView
和ListViewitem
设置inputbinding,该怎么办。。。。
<DataTemplate x:Key="centralTile">
<StackPanel Width="80"
Height="40"
KeyboardNavigation.AcceptsReturn="True">
<StackPanel.InputBindings>
<KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding>
</StackPanel.InputBindings>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
CommandParameter="{Binding}">
<TextBlock Text="{Binding Path=Name}" />
</Button>
<Image Grid.Column="1" Source="Water lilies.jpg" />
</Grid>
<TextBlock HorizontalAlignment="Center"
FontSize="13"
Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
<KeyBinding Key="Enter" Command="{Binding Path=DataContext.KeyCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" CommandParameter="{Binding}"></KeyBinding>