列表框总是自动选择第一项
ListBox 的行为是自动选择第一项,我怎样才能避免这种情况?
注意:我更喜欢使用纯 xaml 来完成此操作,如果您有任何代码隐藏想法,请不要打扰自己。
ListBox's behavior is that the first item is selected automatically, how can I avoid that??
Note: I prefer to do this with pure xaml, if you have any code-behind ideas then please don't bother yourself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试
IsSynchronizedWithCurrentItem="False"
Try
IsSynchronizedWithCurrentItem="False"
好吧,我尝试使用 FocusManager.FocusedElement .. 并将初始焦点设置为
列表框本身..因此它具有焦点..但没有选择任何元素..
如果您按下或按 Tab 键..将选择列表框的第一个元素...
Well i tried this using FocusManager.FocusedElement .. and made the intial focus to
listbox itself.. so it has the focus..but no element is selected..
if u press down or tab ..the 1st element of the listbox will be selected...
删除 IsSynchronizedWithCurrentItem="True" 并根据需要将其添加到下一个 SelectionChanged 事件中。
这解决了我的问题
remove IsSynchronizedWithCurrentItem="True" an add it with the next SelectionChanged event if needed.
This solved my problem
您可以将 SelectedIndex 设置为 -1 :
不幸的是,您无法在 XAML 中完成所有操作...您通常可以避免代码隐藏,但您仍然需要编写转换器、标记扩展或附加属性
You could set SelectedIndex to -1 :
Unfortunately you can't do everything in XAML... you can usually avoid code-behind, but you still need to write converters, markup extensions or attached properties
这是我经常使用的一种技巧。它建立在上面将
FocusedElement
属性添加到Window
或UserControl
的示例之上。我的协议是,我不希望窗口上的任何控件获得焦点。对我来说,解决方案是创建一个没有 UI 的虚拟控件并将焦点分配给它。碰巧
Control
完全符合要求:Here's a technique I use quite often. It builds on the above example of adding the
FocusedElement
attribute to yourWindow
orUserControl
.My deal is that I don't want ANY of the controls on my window to have focus. The solution for me is to create a dummy control that has no UI and assign focus to that. It just so happens that
Control
fits the bill perfectly:SelectedIndex 是您正在寻找的属性吗?或者也许我没明白你的意思...
Is SelectedIndex the property you're looking for ? Or maybe I don't get your point...
这里同样的问题。有人找到“干净”的解决方案吗?
这里的问题是一样的,它会导致一堆触发器执行。
明显的解决方案/修复:
Same issue here. Anyone found a "clean" solution?
The problem is the same here, it causes a bunch of triggers to execute.
Obvious solution/fix:
添加一个空白项目。
Add a blank item.