ListBox的行为是自动选择第一项,我该如何避免这种情况?
注意:我更喜欢使用纯XAML来解决问题,如果您有任何代码后台的想法,请不要费心。
ListBox的行为是自动选择第一项,我该如何避免这种情况?
注意:我更喜欢使用纯XAML来解决问题,如果您有任何代码后台的想法,请不要费心。
Try
IsSynchronizedWithCurrentItem="False"
我尝试使用FocusManager.FocusedElement,将初始焦点设置为listbox本身,这样它就有了焦点,但没有元素被选中。如果按下向下箭头或Tab键,listbox的第一个元素将被选中...
<Window
......
FocusManager.FocusedElement="{Binding ElementName=listbox2}">
<ListBox x:Name="listbox2" HorizontalAlignment="Left"
VerticalAlignment="Bottom" Width="117.333" Height="116"
Margin="30.667,0,0,30">
<ListBoxItem>Jim</ListBoxItem>
<ListBoxItem>Mark</ListBoxItem>
<ListBoxItem>Mandy</ListBoxItem>
</ListBox>
如果需要的话,在下一个SelectionChanged事件中删除IsSynchronizedWithCurrentItem="True"并添加它。这解决了我的问题。
<ListBox ItemsSource="{Binding MyData}" SelectedIndex="-1"/>
<UserControl
x:Class="MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FocusManager.FocusedElement="{Binding ElementName=focus_thief}"
mc:Ignorable="d">
<Grid>
<!-- no renderable UI -->
<Control Name="focus_thief"/>
<!-- wants focus, but won't get it -->
<ListBox>
<ListBoxItem>First Item</ListBoxItem>
</ListBox>
</Grid>
</UserControl>
<ListBox SelectioMode="Single" SelectedIndex="-1"/>
这里也遇到同样的问题。有人找到了“干净”的解决方案吗?
问题在于它导致了一堆触发器的执行。
明显的解决方法/修复方法:
你是在寻找 SelectedIndex 属性吗?或者我没有理解你的意思...
添加一个空白项。