UWP ListView 不固定,宽度变化
Posted
技术标签:
【中文标题】UWP ListView 不固定,宽度变化【英文标题】:UWP ListView is not fixed and its width changes 【发布时间】:2021-11-28 23:09:13 【问题描述】:我创建了一个列表视图,如图所示,列表视图项不是固定的,而是随着 TextBlock 文本而变化。
这是我的列表视图:
<ListView x:Name="listView"
ItemContainerStyle="StaticResource ListViewItemWithoutHover"
ItemsSource="x:Bind SubtitlesACV, Mode=OneWay"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.IsHorizontalRailEnabled="True"
SelectedIndex="-1"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:SubsceneDownloadModel">
<usercontrol:SubsceneUserControl.../>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
和用户控件是:
<Grid MinHeight="68"
Padding="0,0,16,0"
HorizontalAlignment="Stretch"
Background="ThemeResource CardBackgroundBrush"
BorderBrush="ThemeResource CardBorderBrush"
BorderThickness="ThemeResource CardBorderThickness"
CornerRadius="ThemeResource ControlCornerRadius">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="52" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1"
Margin="0,0,16,0"
VerticalAlignment="Center"
Orientation="Vertical">
<TextBlock FontSize="16"
FontWeight="SemiBold"
Text="Binding ElementName=subsceneView, Path=Title"
TextWrapping="Wrap" />
....
<StackPanel Grid.Column="2"
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="8">
<Button Name="DownloadButton"
Width="40"
Height="36"
Background="Transparent"
Click="DownloadButton_Click"
Content=""
FontFamily="Segoe MDL2 Assets">
<ToolTipService.ToolTip>
<TextBlock Text="Download Subtitle" />
</ToolTipService.ToolTip>
</Button>
...
</StackPanel>
</Grid>
这是我的ItemContainerStyle
<Style x:Key="ListViewItemWithoutHover" TargetType="ListViewItem">
<Setter Property="Margin" Value="0,0,0,2"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
问题是即使我为Textblock设置了TextWrapping="Wrap"属性,它仍然会导致listviewitem不稳定并发生变化。我希望 listviewitem 的宽度是固定的
【问题讨论】:
【参考方案1】:删除这 2 行
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
【讨论】:
以上是关于UWP ListView 不固定,宽度变化的主要内容,如果未能解决你的问题,请参考以下文章
Listview UWP 的自动宽度(将最宽行的宽度设置为所有其他行)