【问题标题】:Aligment of Items in nested ItemsControls嵌套 ItemsControl 中项目的对齐方式
【发布时间】:2015-10-21 06:38:24
【问题描述】:

我有两个 ItemsControls 嵌套在一个 ItemsControl 中。每个都在网格列中彼此相邻放置,具有水平方向的 StackPanel ItemsPanelTemplates,因此它们的内容水平分层。

虽然我希望两个 ItemsControls 占据父项的整个宽度 (50:50),但我希望它们中的项目分别是右对齐和左对齐...所以它们都居中,类似于 (请原谅我尝试 ASCII 艺术):

|     LH ItemsControl           |     RH ItemsControl       |
|                       [][][][]|[][][]                     |

到目前为止,这是我的代码,我一直在调整 Horizo​​ntalAlignment 属性,但如果我让它们占据中心,那么两个 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 面板上的 Horizo​​ntalAlignment="Left"。和 R ItemsControl 面板上的 orizo​​ntalAlignment="Right" ?仅供参考,Stretch 是您不需要显式设置的默认值。
  • 感谢@eranotzap 的回复,但恐怕这会给我正确布局的项目(彼此居中),但 StackPanel 并没有占据父级的整个宽度。跨度>
  • 你为什么需要它来......? StackPanel 只占用需要的空间..
  • 公平地说,我希望能够在 ItemsControl 可以占据的空间而不是项目本身上设置背景颜色。而是在 ItemsControl 上设置了 Background 属性,这就是我所追求的,干杯。
  • 和设置列表框背景不起作用?

标签: c# wpf itemscontrol


【解决方案1】:

在 ItemsControl 而不是 StackPanel 上设置 Background 属性并在 StackPanel 上分别将 Orientation 设置为 Left 和 Right 给了我想要的效果:

<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" Background="LightPink">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Height="37"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
            <ItemsControl Grid.Column="1" ItemsSource="{Binding RightThings}" HorizontalAlignment="Stretch" Background="LightBlue">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="37"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

【讨论】:

    【解决方案2】:

    在左侧堆栈面板上使用 FlowDirection=RightToLeft,在 DataTemplate 控件中使用 FlowDirection=LeftToRight。我做了一个样品。下面的代码可以按原样使用:

    MainWindow.xaml

    <Window x:Class="WpfItemsControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="407.895" Width="884.211">
    <Grid>
    
        <ItemsControl ItemsSource="{Binding Names}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <ItemsControl  ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" Grid.Column="0">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" Background="Red" HorizontalAlignment="Stretch" FlowDirection="RightToLeft">
                                    </StackPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Background="DarkGoldenrod" FontSize="25" FontWeight="Bold" Foreground="Gray" Text="{Binding}" FlowDirection="LeftToRight"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <ItemsControl  ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" Grid.Column="1">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" Background="Red" HorizontalAlignment="Stretch" FlowDirection="LeftToRight">
                                        </StackPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Background="DarkGoldenrod" FontSize="25" FontWeight="Bold" Foreground="Gray" Text="{Binding}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>            
        </ItemsControl>     
    
    </Grid>
     </Window>
    

    MainWindow.xaml.cs

    namespace WpfItemsControl
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            try
            {
                InitializeComponent();
    
                this.DataContext = new DataStore();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.InnerException.Message);
            }
        }
    }
    
    public class DataStore
    {
        public List<string> Names { get; set; }
    
        public DataStore()
        {
            Names = new List<string>();
    
            Names.Add(">");
            Names.Add(">");
            Names.Add(">");
            Names.Add(">");
            Names.Add(">");
        }
    }
    

    }

    此代码将两个侧面项目置于中心,并拉伸两个堆栈面板。

    【讨论】:

    • 不要使用流向来对齐元素,这会改变元素的布局方向。只需将“>”名称更改为不同的值,例如“1”“2”“3”“4”“5”,看看它的作用。
    • 感谢您的回复和示例,但正如@NovitchiS 所说,这颠倒了我不想发生的顺序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多