C# 无法使用DataTemplate WPF获取InputBinding以应用于整个ListBoxItem
试图在WPF中的ListBox中获取一些键绑定到我的ListBoxItems。我正在使用MVVM,并将ListBox的ItemSource绑定到ViewModels列表。此ViewModel有一个字符串和一个“Selected”布尔值。我希望将所选内容显示为复选框的属性 我正在尝试这样做,如果我使用键盘上的上下箭头导航列表项,然后按enter/space/任意键,我可以切换复选框。但是,我必须先按tab键,才能将焦点转到包含复选框的StackPanelC# 无法使用DataTemplate WPF获取InputBinding以应用于整个ListBoxItem,c#,wpf,xaml,mvvm,command,C#,Wpf,Xaml,Mvvm,Command,试图在WPF中的ListBox中获取一些键绑定到我的ListBoxItems。我正在使用MVVM,并将ListBox的ItemSource绑定到ViewModels列表。此ViewModel有一个字符串和一个“Selected”布尔值。我希望将所选内容显示为复选框的属性 我正在尝试这样做,如果我使用键盘上的上下箭头导航列表项,然后按enter/space/任意键,我可以切换复选框。但是,我必须先按tab键,才能将焦点转到包含复选框的StackPanel <DataTemplate x:Ke
<DataTemplate x:Key="MyTemplate" DataType="{x:Type ViewModel}">
<Border Width="2" BorderBrush="Blue">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding EnterCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<CheckBox VerticalAlignment="Center"
Content="{Binding Name}"
IsChecked="{Binding Selected}"
Margin="3" />
</Border>
</DataTemplate>
=======================
<Popup x:Name="FilterPopup" Grid.Column="1"
IsOpen="{Binding IsChecked, ElementName=FilterButton}"
StaysOpen="False"
PlacementTarget="{Binding ElementName=FilterButton}"
Placement="Top">
<ListBox ItemsSource="{Binding ViewModels}"
ItemTemplate="{StaticResource MyTemplate}" />
</Popup>
我是否错过了一些明显的东西?上面的触发器是在数据模板中触发的,而不是在项目容器中触发的。因此,如果最后一个被聚焦,就没有效果 为避免这种情况,应在项目容器级别指定触发器:
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="l:Attach.InputBindings">
<Setter.Value>
<InputBindingCollection>
<KeyBinding Command="{Binding EnterCommand}" Key="Enter" />
<KeyBinding Command="{Binding EnterCommand}" Key="Space" />
</InputBindingCollection>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
我已经采用了一种从样式设置输入绑定的方法