我目前正在使用ListView。它的ListViewItems包含一个左对齐的TextBlock和一个右对齐的Button:
现在,我想让我的Items 总是显示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>
感谢任何提示! : - )
答案 0 :(得分:4)
您可以通过在按钮列上强制调整大小来执行此操作:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="JansWpfTestUmgebung.MainWindow"
d:DesignWidth="394">
<ListView x:Name="AllItemsList"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="45" />
</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 Content="Item 2"/>
<ListViewItem Content="Item 3"/>
</ListView>
</Window>
它的方式是,它尽可能保持*大小的列尽可能大,代价是自动调整大小的列。但固定大小将胜过*大小。
答案 1 :(得分:0)
最重要的部分是这些
<ListView HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>
这样你的ListViewItems将填充空间,禁用的水平滚动条将阻止它们占用更多空间。
我的代码看起来像这样
<ListView HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding MyItemSourceList}"
Width="{Binding ActualWidth, ElementName=SearchBox}"
>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding Name}"
TextTrimming="CharacterEllipsis"/>
<TextBlock Grid.Column="1"
Text="{Binding Tag}"
HorizontalAlignment="Right"
FontWeight="Bold"
Foreground="LightGray"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>