Yoh*_*hyo 1 c# wpf xaml listbox
我创建按钮作为ListBox项目.使用键盘快捷键,所选项目/按钮将更改为第一个字符与按下的键相同的按钮.问题是焦点项(虚线矩形)不会与所选项同步.如果我们使用键盘箭头,则不存在此问题.短代码是:
<ListBox x:Name="ListBoxRef"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
DataContext="{Binding Path=ListViewSource}" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding}" IsTabStop="False"
Command="{Binding ElementName=UserControlTypeSelectionView, Path=DataContext.SelectCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
<Button.Template>
<ControlTemplate>
<TextBlock Text="{Binding Path=Name}" />
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
在如何以焦点方式将焦点设置为已具有焦点的WPF ListBox中的SelectedItem?,解决方案是使用Focus方法和C#方面.
是否可以在MVVM中仅使用XAML?
我找到了同事的答案.通过使用当前所选项目设置器(IsSelected属性)触发FocusManager.FocusedElement,问题得以解决.
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<!-- Edited: add pushpraj code to hide the dashed rectangle on focused item -->
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4106 次 |
| 最近记录: |