如何对齐ItemsControl控件(WPF)的列

本文关键字:的列 控件 WPF ItemsControl 对齐 何对齐 | 更新日期: 2023-09-27 17:54:57

我在xaml中定义了一个ItemsControl控件,如下所示:

<ItemsControl BorderThickness="0"  Name="SummaryList">
</ItemsControl>

在同一个xaml文件中,我为ItemsControl中的每个项目定义了以下DateTemplate:

<DataTemplate DataType="{x:Type app:UpgradeItem}">
    <StackPanel Orientation="Horizontal">
        <Label Margin="5,5" Content="{Binding Path=ItemText,Mode=OneTime}" />
        <Label Margin="5,5" Content="{Binding Path=FromVersion,Mode=OneTime}" />
        <Label Margin="5,5" Content="{Binding Path=ToVersion,Mode=OneTime}" />
    </StackPanel>
</DataTemplate>

我已经将ItemsControl的ItemSource绑定到UpgradeItems的一般列表(即list),并在执行期间以编程方式添加到该列表。ItemsControl控件中的项正确填充,只是列中的数据没有对齐。我如何修改这使三个单独的列(ItemText, FromVersion和ToVersion)对齐?

TIA

如何对齐ItemsControl控件(WPF)的列

您应该能够在网格上使用SharedSize组,但是我觉得有更好的方法来做到这一点。这里有一个关于这个主题的链接:http://blogs.microsoft.co.il/blogs/guy/archive/2009/06/30/wpf-grid-shared-size-groups.aspx

让这个问题变得棘手的部分原因是ItemsControl没有 "列":控件本身不知道其项的内容是如何相互布局的。

您最好的选择可能是使用ItemsControl的一个子类,它支持列数据,如ListView/GridView或成熟的DataGrid。