将列表框项目的命中区域拉伸到列表框的全宽? ListBox 样式通过主题隐式设置
我已经四处寻找对此的答案,但潜在的重复更关心的是演示而不是交互。
我有一个基本的列表框,每个项目的内容都是一个简单的字符串。
ListBox 本身被拉伸以填充其网格容器,但每个 ListBoxItem 的 hitarea 并不反映 ListBox 的宽度。看起来好像每个项目的 hitarea(指针接触区域)只是文本内容的宽度。无论文本大小如何,如何使其一直延伸。
我已将 HorizontalContentAlignment 设置为 Stretch,但这并不能解决我的问题。我唯一的另一个猜测是内容实际上正在拉伸,但背景是不可见的,因此不会捕获鼠标指针。
<ListBox
Grid.Row="1"
x:Name="ProjectsListBox"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=Projects}"
SelectedItem="{Binding Path=SelectedProject}"
HorizontalContentAlignment="Stretch"/>
XAML 在这方面非常简单。如果我将鼠标悬停在其中一项中的文本上,则该项的整个宽度将变为活动状态。我想我只需要知道如何创建一个不可见的交互式背景。
I've looked around for an answer on this, but the potential duplicates are more concerned with presentation than interaction.
I have a basic list box, and each item's content is a simple string.
The ListBox itself is stretched to fill it's grid container, but each ListBoxItem's hitarea does not mirror the ListBox width. It looks as if the hitarea (pointer contact area) for each item is only the width of the text content. How do I make this stretch all the way across, regardless of the text size.
I've set HorizontalContentAlignment to Stretch, but this doesn't solve my problem. My only other guess is that the content is actually stretching, but the background is invisible and so not capturing the mouse pointer.
<ListBox
Grid.Row="1"
x:Name="ProjectsListBox"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=Projects}"
SelectedItem="{Binding Path=SelectedProject}"
HorizontalContentAlignment="Stretch"/>
The XAML is pretty straight forward on this. If I mouse over the text in one of the items, then the entire width of the item becomes active. I guess I just need to know how to create an interactive background that is invisible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应在 ItemContainerStyle 中设置
HorizontalContentAlignment="Stretch"
才能正常工作。Xaml 示例
更新
试试这个
HorizontalContentAlignment="Stretch"
should be set in ItemContainerStyle for this to work.Xaml Example
Update
Try this
另外,添加到 Fredrik 的答案中,如果将 ListBox 的
ScrollViewer.HorizontalScrollBarVisibility
属性设置为Disabled
,则项目将始终具有 ListBox 的客户端宽度。Also, adding to Fredrik's answer, if you set the ListBox's
ScrollViewer.HorizontalScrollBarVisibility
property toDisabled
, the items will always have exactly the client width of the ListBox.