【发布时间】:2015-10-21 06:38:24
【问题描述】:
我有两个 ItemsControls 嵌套在一个 ItemsControl 中。每个都在网格列中彼此相邻放置,具有水平方向的 StackPanel ItemsPanelTemplates,因此它们的内容水平分层。
虽然我希望两个 ItemsControls 占据父项的整个宽度 (50:50),但我希望它们中的项目分别是右对齐和左对齐...所以它们都居中,类似于 (请原谅我尝试 ASCII 艺术):
| LH ItemsControl | RH ItemsControl |
| [][][][]|[][][] |
到目前为止,这是我的代码,我一直在调整 HorizontalAlignment 属性,但如果我让它们占据中心,那么两个 StackPanel 不会填充父级的整个宽度。
<ItemsControl ItemsSource="{Binding Things}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ItemsControl Grid.Column="0" ItemsSource="{Binding LeftThings}" HorizontalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Background="LightPink" HorizontalAlignment="Stretch" Height="37"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ItemsControl Grid.Column="1" ItemsSource="{Binding RightThings}" HorizontalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Background="LightBlue" HorizontalAlignment="Stretch" Height="37"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
有什么想法吗?
丰富
【问题讨论】:
-
您是否尝试过 L ItemsControl 面板上的 HorizontalAlignment="Left"。和 R ItemsControl 面板上的 orizontalAlignment="Right" ?仅供参考,Stretch 是您不需要显式设置的默认值。
-
感谢@eranotzap 的回复,但恐怕这会给我正确布局的项目(彼此居中),但 StackPanel 并没有占据父级的整个宽度。跨度>
-
你为什么需要它来......? StackPanel 只占用需要的空间..
-
公平地说,我希望能够在 ItemsControl 可以占据的空间而不是项目本身上设置背景颜色。而是在 ItemsControl 上设置了 Background 属性,这就是我所追求的,干杯。
-
和设置列表框背景不起作用?
标签: c# wpf itemscontrol