嵌套数据绑定与C#WPF中的ItemsControl对齐 [英] Alignment of nested databinding with ItemsControl in C# WPF

查看:48
本文介绍了嵌套数据绑定与C#WPF中的ItemsControl对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据模板:

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

这包括该组中的一些ButtonGroup和Buttons.

This includes some ButtonGroups and Buttons which are in this Group.

类Group包括字符串属性"GroupName"和ObservableCollection属性"Buttons".按钮和组的分配工作正常.

The class Group includes a string-property "GroupName" and an ObservableCollection-property "Buttons". The allocation of buttons and groups is working correctly.

这是我的问题:我想在 dockpanel ribbontab 中使用此按钮组.但是对齐方式或方向是错误的,因此按钮位于另一个下方,而不是彼此相邻. 有谁知道我的代码有什么问题吗?

So here is my problem: I want to have this buttongroups in a ribbontab in the dockpanel. But the alignment or the orientation is false, so the buttons are one below the other and not next to each other. Has anyone an idea what is wrong in my code?

推荐答案

目前,您使用的是水平方向的Stackpanel,这是正确的主意,但Stackpanel放在错误的位置( ItemTemplate). ItemTemplate应用于ItemsControl中的每个项目,这意味着您的XAML表示按钮的集合,其中每个按钮都被其自己的StackPanel包围.

At the moment, you're using a Stackpanel with horizontal orientation, which is the right idea, but the Stackpanel is in the wrong place (the ItemTemplate). The ItemTemplate is applied to every item in an ItemsControl, which means that your XAML represents a collection of buttons where each is surrounded by its very own StackPanel.

要获得理想的效果,您需要将Stackpanel指定为ItemsControlItemsPanelTemplate.

To have the desired effect, you instead need to specify the Stackpanel as the ItemsPanelTemplate of the ItemsControl.

尝试将您的内部子句更改为:

Try changing your inner clause to:

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

修改

如果要同时显示组和按钮,则可以对两者进行相同的操作:

If you want both groups and buttons to display horizontally, you can do the same thing to both:

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

这篇关于嵌套数据绑定与C#WPF中的ItemsControl对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆