我希望 ListView
的项目能够增长以占据 ListView
内可用空间的整个宽度。
目前,我将项目的宽度设置如下(ListView
名为 listView):
Width="{Binding ActualWidth, ElementName=listView, Mode=OneWay}"
这些项目将占据ListView
的宽度。但是,没有考虑垂直滚动条。如果存在滚动条,它将绘制在项目右边缘的顶部。
可以解决这个问题吗?
最佳答案
我通过直接引用 ItemsPresenter 解决了这个问题。为此,我需要为 Viewbox 定义一个模板并将其命名为 ItemsPresenter。
模板如下所示:
<ListView.Template>
<ControlTemplate TargetType="{x:Type ListView}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
<ItemsPresenter x:Name="MLVItemsPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
<--SNIP-->
</ControlTemplate>
</ListView.Template>
宽度绑定(bind)如下所示:
Width="{Binding ActualWidth, ElementName=MLVItemsPresenter, Mode=OneWay}"
关于wpf - 将列表项设置为增长到包含的 ListView 的宽度 - WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3347891/