C#WPF中嵌套数据绑定与ItemsControl的对齐

C#WPF中嵌套数据绑定与ItemsControl的对齐,c#,wpf,xaml,mvvm,itemscontrol,C#,Wpf,Xaml,Mvvm,Itemscontrol,我有以下数据模板: <ItemsControl x:Name="Groups" ItemsSource="{Binding Groups}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel x:Name="GroupStackPanel" Orientation="Horizontal">

我有以下数据模板:

<ItemsControl x:Name="Groups" ItemsSource="{Binding Groups}">
    <ItemsControl.ItemTemplate>
         <DataTemplate>
               <StackPanel x:Name="GroupStackPanel" Orientation="Horizontal">
                    <GroupBox Header="{Binding Path=GroupName}">
                         <ItemsControl ItemsSource="{Binding Buttons}">
                               <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                         <StackPanel x:Name="BtnStackPanel" Orientation="Horizontal">
                                               <Button Content="{Binding Path=LabelString}"
                                                                Command="{Binding Path=ButtonCommand}"/>
                                          </StackPanel>
                                     </DataTemplate>
                                </ItemsControl.ItemTemplate>
                           </ItemsControl>
                      </GroupBox>
                 </StackPanel>
           </DataTemplate>
     </ItemsControl.ItemTemplate>
</ItemsControl>

这包括一些按钮组和此组中的按钮

类组包括一个字符串属性“GroupName”和一个ObservableCollection属性“Buttons”。按钮和组的分配工作正常

这就是我的问题:我想在dockpanel中的ribbontab中对这个按钮进行分组。但是对齐或方向是错误的,因此按钮一个接一个,而不是相邻。
有人知道我的代码出了什么问题吗?

目前,您使用的是水平方向的
Stackpanel
,这是正确的想法,但
Stackpanel
位于错误的位置(
ItemTemplate
)。
itemstemplate
应用于
ItemsControl
中的每个项目,这意味着您的XAML表示一组按钮,其中每个按钮都由自己的
堆栈面板
包围

要获得所需的效果,您需要将
Stackpanel
指定为
ItemsControl
ItemsPanelTemplate

尝试将内部子句更改为:

<ItemsControl ItemsSource="{Binding Buttons}">
   <ItemsControl.ItemTemplate>
     <DataTemplate>
       <Button Content="{Binding Path=LabelString}" Command="{Binding Path=ButtonCommand}"/>
     </DataTemplate>
   <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
       <StackPanel x:Name="BtnStackPanel" Orientation="Horizontal">
     </ItemsPanelTemplate>  
   <ItemsControl.ItemsPanel>
</ItemsControl>

编辑

如果希望两个组和按钮都水平显示,可以对这两个组和按钮执行相同的操作:

 <ItemsControl x:Name="Groups" ItemsSource="{Binding Groups}">
   <ItemsControl.ItemTemplate>
     <DataTemplate>         
       <GroupBox Header="{Binding Path=GroupName}">
          <ItemsControl ItemsSource="{Binding Buttons}">
             <ItemsControl.ItemTemplate>
               <DataTemplate>
                 <Button Content="{Binding Path=LabelString}" Command="{Binding Path=ButtonCommand}"/>
               </DataTemplate>
             <ItemsControl.ItemsPanel>
               <ItemsPanelTemplate>
                 <StackPanel x:Name="BtnStackPanel" Orientation="Horizontal"/>
               </ItemsPanelTemplate>  
             <ItemsControl.ItemsPanel>
          </ItemsControl>
        </GroupBox>
     </DataTemplate>
   </ItemsControl.ItemTemplate>
   <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
       <StackPanel x:Name="GroupStackPanel" Orientation="Horizontal"/>
     </ItemsPanelTemplate>
   <ItemsControl.ItemsPanel>
 </ItemsControl>


谢谢。看起来不错。因此,现在按钮彼此相邻,但组仍在彼此下方。是的-如果需要,可以对组执行类似的操作:)是的,我这样做了,但现在我得到一个例外:“panel”的子集合,用作“ItemsControl”的“ItemsPanel”,无法显式更改我已经编辑了我的示例来实现这一点——也许可以检查一下您是否有相同的示例?我最初没有发布这篇文章,因为我认为它会一下子变得有点多,而且语法更清晰,只需要一个汉克就可以了。现在它起作用了。我想我总是用完全不同的方式:)