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

时间:2011-05-04 15:37:19

标签: c# wpf wpf-controls itemscontrol

我在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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

使这一点变得棘手的部分原因是ItemsControl没有拥有“列”:控件本身不知道其项目的内容是如何根据每个项目布局的其他。

您最好的选择可能是使用支持列式数据的ItemsControl子类之一,例如ListView/GridView或完整的DataGrid