WPF:如何让 ListViewItems 适合窗口宽度并使用 TextEllipsis
我目前正在使用 ListView。它的 ListViewItems 由左对齐的 TextBlock 和右对齐的 Button 组成:
现在,我想让我的项目始终显示按钮并相应地缩短 TextBlock,这样它们就不需要显示 ScrollBar:
不幸的是,现在这不起作用:
我该怎么做才能使其正常工作? 这是我的示例代码:
<Window x:Class="JansWpfTestUmgebung.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView x:Name="AllItemsList"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch">
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
TextTrimming="CharacterEllipsis"
Text="Item 1 with a very long description"/>
<Button Grid.Column="1"
Content="Modify" />
</Grid>
</ListViewItem>
<ListViewItem>Item 2</ListViewItem>
<ListViewItem>Item 3</ListViewItem>
</ListView>
</Window>
感谢您的任何提示! :-)
I'm currently working with a ListView. Its ListViewItems consist of a left-aligned TextBlock and a right-aligned Button:
Now, I'd like to have my Items always display the Button and shorten the TextBlock accordingly, so they don't need to display a ScrollBar:
Unfortunately, right now that doesn't work:
What can I do to make it work?
Here's my example code:
<Window x:Class="JansWpfTestUmgebung.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView x:Name="AllItemsList"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch">
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
TextTrimming="CharacterEllipsis"
Text="Item 1 with a very long description"/>
<Button Grid.Column="1"
Content="Modify" />
</Grid>
</ListViewItem>
<ListViewItem>Item 2</ListViewItem>
<ListViewItem>Item 3</ListViewItem>
</ListView>
</Window>
Thanks for any hints! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过强制按钮列的大小来完成此操作:
按照过去的方式,它会尽力保持 * 大小的列尽可能大,但会牺牲自动调整大小的列。但固定尺寸会胜过 * 尺寸。
You can do this by forcing a size on the button column:
The way it was, It does its best to keep the * sized column as big as possible, at the expense of the Auto sized column. But a fixed size will win out against a * sized.
最重要的部分是这些,
这样您的 ListViewItems 将填充空间,并且禁用的水平滚动条将阻止它们占用更多空间。
我的代码看起来像这样
The Most important Parts are these
That way your ListViewItems will fill the space, and the disabled horizontal scroll bar will prevent them from taking up more space.
My code looks like this