c# - 防止 ListBox 中的项目超出其宽度

标签 c# wpf listbox itemscontrol listboxitem

我确信这很简单,但我似乎无法弄明白。基本上,我试图强制 ListBoxItem 允许自己超出 ListBox 宽度。例如,假设我们有一个原始的 ListBox,每个 ListBoxItem 都有一个 TextBox:

<ListBox HorizontalContentAlignment="Stretch">
    <ListBoxItem>
        <TextBox TextWrapping="Wrap"/>
    </ListBoxItem>
    <ListBoxItem>
        <TextBox TextWrapping="Wrap"/>
    </ListBoxItem>
    <ListBoxItem>
        <TextBox TextWrapping="Wrap"/>
    </ListBoxItem>
</ListBox>

如果您要输入它们,即使文本将 TextWrapping 设置为 Wrap,文本仍将继续向右流动,因为 的宽度>ListBoxItemListBox 调整内容的宽度(注意 Horizo​​ntalScrollBar 出现):

enter image description here

我确定这是预期的行为,可能是由模板中的 ScrollViewer 引起的,但我希望文本换行并包含在原始宽度内。我可以通过设置静态宽度来解决这个问题,但这将是一个粗略的设计决定,而不是我想做的事情(它会限制事物轻易调整大小)。

基本上,我正在寻找的行为是 ItemsControl 的行为:

<ItemsControl>
    <TextBox TextWrapping="Wrap"/>
    <TextBox TextWrapping="Wrap"/>
    <TextBox TextWrapping="Wrap"/>
</ItemsControl>

enter image description here

我只想保留 ListBox 控件,因为它的其他行为是我需要的。有什么想法吗?

最佳答案

这将禁用水平滚动(即使听起来它只是禁用可见性):

<ListBox HorizontalContentAlignment="Stretch"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled">

关于c# - 防止 ListBox 中的项目超出其宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096551/

相关文章:

c# - 系统.NullReferenceException : Object reference not set to an instance of an object in C#

c# - 全局常量可能吗?

c# - “不支持返回 System.IServiceProvider 的 ConfigureServices。”

c# - 更改绑定(bind)的一部分

c# - Entity Framework 7 和 SQLite 的组合键

.net - 如何在 Visual C++ .net 中将一个项目从一个列表框转移到另一个

arrays - 将用户窗体中> 10列的多列列表框复制到数组

c# - 如何测试向量3是否位于围绕特定点旋转的立方体内部

C# OneDrive for Business/SharePoint : get server path from locally synced file

image - 从隔离存储延迟加载列表框图像