wpf - wpf 中相同大小的列表框项目

标签 wpf xaml

如何使ListBox内的ListBoxItems具有相同的高度?

<ListBox
    HorizontalContentAlignment="Stretch"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    >
    <ListBoxItem><TextBlock TextWrapping="Wrap">Long text that would wrap and increase height of single ListBoxItem</TextBlock></ListBoxItem>
    <ListBoxItem><TextBlock TextWrapping="Wrap">Short text</TextBlock></ListBoxItem>
    <ListBoxItem><TextBlock TextWrapping="Wrap">Short text</TextBlock></ListBoxItem>
</ListBox>

我希望具有“短文本”的项目与第一个项目具有相同的高度(由于换行,通常会具有更多行)。

最佳答案

我在这里有点作弊,但试试这个:

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <UniformGrid Rows="{Binding Path=Items.Count, RelativeSource={RelativeSource AncestorType=ListBox}}" VerticalAlignment="Top"/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
    <TextBlock TextWrapping="Wrap">Long text that would wrap and increase height of single ListBoxItem</TextBlock>
</ListBoxItem>
<ListBoxItem>
    <TextBlock TextWrapping="Wrap">Short text</TextBlock>
</ListBoxItem>
<ListBoxItem>
    <TextBlock TextWrapping="Wrap">Short text</TextBlock>
</ListBoxItem>

关于wpf - wpf 中相同大小的列表框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12998300/

相关文章:

.net - 使用多个模板的 WPF 控件创作

wpf - 按钮不覆盖父级的 IsEnabled

wpf - 在 'System.Windows.StaticResourceExtension' 上提供值引发异常

c# - 在 WPF 中使用字典进行双向数据绑定(bind)

WPF 可执行文件不会在 Visual Studio 之外运行(资源字典问题)

c# - 绑定(bind)到列表框时出现 XamlParseException

c# - 拖动和滚动 (WPF)

c# - DelegateCommand.CanExecute 方法行为不正确

wpf - 如何知 Prop 有扩展选择模式的DataGrid中的选定项目?

c# - 带有对象实例的 XAML 中的 ObjectDataProvider 不会在调试中调用 ViewModel 构造函数