You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XAML中移除ListView右侧的空白列?

解决方案(纯XAML实现)

可以通过纯XAML移除右侧空白,核心是改变GridView默认填充ListView整个宽度的行为,以下是两种可行方案:

方案一:简单设置内容对齐属性

直接给目标ListView添加HorizontalContentAlignment="Left"属性,让GridView仅以列宽总和展示,不自动拉伸填充剩余空间:

<ListView
    Name="lstMovies"
    Grid.Row="1"
    Grid.Column="1"
    FlowDirection="LeftToRight"
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="{Binding Movies}"
    SelectedItem="{Binding CurrentMovie, Mode=TwoWay}"
    SelectionChanged="lstMovies_SelectionChanged"
    SelectionMode="Single"
    HorizontalContentAlignment="Left"> <!-- 添加该属性 -->
    <!-- 原有View及列配置保持不变 -->
</ListView>

方案二:重写ListView模板(精准控制)

如果方案一无法满足需求,可通过重写ListView的ControlTemplate,强制内部内容左对齐,彻底避免自动拉伸:

<Style TargetType="ListView">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListView">
                <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        Padding="{TemplateBinding Padding}" 
                        SnapsToDevicePixels="True">
                    <ScrollViewer Focusable="False" Padding="{TemplateBinding Padding}">
                        <ItemsPresenter HorizontalAlignment="Left" /> <!-- 设置左对齐 -->
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

说明

你之前设置的ListViewItem.HorizontalContentAlignment="Center"仅控制每行内容在列内的对齐方式,和GridView整体拉伸导致的右侧空白无关,无需修改这部分配置。

内容的提问来源于stack exchange,提问作者Aleksandar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:54:56