WPF,ListBox的ItemTemplate有CheckBox,但是CheckBox好像不是item
我只想要以前与 Windows 窗体一起使用的 CheckListBox。
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
起初这似乎可行,但出现了很多问题。简而言之,它的工作原理就像一个 CheckBox 漂浮在真实的项目上,而不是 CheckBox 就是该项目。
我的意思是,(1)单击复选框的文本不会选择列表框项目,(2)按向上和向下键不会聚焦复选框。我必须单击该复选框才能将其聚焦。我在谷歌上搜索了解决方案,但没有干净的解决方案。是我想要的太多了吗?
我只想要 CheckedListBox 的行为...
我通过处理复选框的 PreviewMouseDown 事件并手动选择该项目来解决 (1) 问题。看起来不太干净。
I just wanted the CheckListBox I used to use with Windows Forms.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
At first this seemed to work, but there was numerous problems. In short, it just works like a CheckBox is floating on the real item, rather than the CheckBox is the item.
I mean, (1)clicking the checkbox's text would not select the ListBox item, (2)pressing up and down key does not focus the checkbox. I have to click the checkbox in order to focus it. I have searched Google for solutions but there weren't clean solutions. Am I wanting too much?
I just want the behavour of CheckedListBox...
I worked around (1) by handling the PreviewMouseDown event of the checkbox and manually selecting the item. It does not seem to be clean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的
CheckBox
位于ListBox
中。它被作为具有所有功能的列表中的一项进行处理。如果您只想构建复选框列表并且不需要列表的选择逻辑,请使用 ItemsControl 而不是
ListBox
。用法是相等的。如果您想让 CheckboxList 可滚动,请使用 ScrollViewer 来包装 ItemsControl。This is, because your
CheckBox
is in aListBox
. It is handled as an item of the list with all it's features.If you want to build only a list of checkboxes and don't need selection-logic of the list, use an ItemsControl instead of the
ListBox
. The usage is equal. If you want to have your CheckboxList scrollable, use ScrollViewer to wrap the ItemsControl.链接问题的选定答案(WPF ListBoxItem 选择问题)提供了一个干净的解决方案!陷入同样的场景 ->找到了你的问题->找到了另一个有补救措施的人。哈!
Selected answer for the linked question (WPF ListBoxItem selection problem) provides a clean solution! Was stuck with the same scenario -> found your question -> found the other one with the remedy. HTH!