![](/img/trans.png)
[英]Change the Forecolor of the ListBoxItem through DataTemplate in WPF
[英]Cant get InputBinding to apply to entire ListBoxItem using DataTemplate WPF
試圖在WPF中的ListBox中將一些鍵綁定到我的ListBoxItems上。 我正在使用MVVM,並將ListBox的ItemSource綁定到ViewModel列表。 此ViewModel具有字符串和“已選擇”的布爾值。 我希望將Selected顯示為CheckBox的屬性。
我試圖這樣做,如果我用鍵盤上的向上和向下箭頭導航列表項,然后按Enter / space / what,我可以切換復選框。 但是,我必須先按Tab鍵才能將焦點集中到包含復選框的StackPanel。
<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>
我已經采取了一種方法來設置來自這個問題的樣式的輸入綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.