仅列表框项目中的文本可选择,文本之外的空间不可选择
我有一个 WPF ListBox,其项目是 TextBlocks。当我单击文本时,SelectionChanged 处理程序将按预期调用。但是,如果我在项目内部单击,但不是直接在文本上单击,则不会调用处理程序。当文本项的长度变化很大时,这一点更加明显。如果我有两个项目:
foo
感叹号
“foo”项目右侧有很多空间,无法响应点击
<DataTemplate x:Key="NameTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
...
<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>
I have a WPF ListBox whose items are TextBlocks. When I click on the text the SelectionChanged handler is called as expected. However, if I click inside the item, but not directly over the text the handler is not called. This is more apparent when the text items are of widely varying lengths. If I have two items:
foo
exclamation
The "foo" item has a lot of space to the right which doesn't respond to the click
<DataTemplate x:Key="NameTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
...
<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource NameTemplate}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现以下内容有效,但看起来相当冗长......
关于如何更简洁地做到这一点有什么想法吗?或者将其放入 ItemTemplate 中的方法?我找不到在模板中执行相同操作的方法。
没有它的原版只是:
I found that the following works, but it seems rather verbose...
Any ideas on how to do this more concisely? Or a way to put this in the ItemTemplate? I couldn't find a way to do the same in the template.
The orig without that was just:
尝试。您可以去掉背景颜色,但这会显示 TextBlock 有多大。
Try. You can take away the background color but that will show you how big the TextBlock is.
您确定您单击的额外空白区域在您的
ListBox
“内部”吗?您确定您的ListBox
跨越了那么大的宽度吗?因为在我的情况下似乎没有发生......(以下
ListBox
是Window
的子级)我的
ListBox_SelectionChanged
甚至被正确调用如果我单击项目级别TextBlock
边界之外的空白区域(前提是我实际上单击ListBox
内部某处)。Are you sure the extra white space you are clickin on is "inside" your
ListBox
. Are you sure yourListBox
is spanned across that much width?Coz it doesnt seem to happen in my case.... (following
ListBox
is a child of aWindow
)My
ListBox_SelectionChanged
is correctly called even if I click the white space outside the item levelTextBlock
boundaries (provided that I am actually clicking somewhere inside theListBox
).