代码之家  ›  专栏  ›  技术社区  ›  harriyott Erik Funkenbusch

WPF ItemsControl数据绑定-添加额外的初始控件

  •  0
  • harriyott Erik Funkenbusch  · 技术社区  · 15 年前

    对于databound items控件,我使用以下XAML来显示组列表,并使用标签和按钮首先添加新组:

    <WrapPanel Orientation="Horizontal">
        <Label Content="Groups" />
        <Button x:Name="btnGroupAdd" Click="btnGroupAdd_Click" Content="+" />
        <ItemsControl ItemsSource="{Binding}" x:Name="_groupList" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal">
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Padding="3" BorderThickness="1">
                        <WrapPanel>
                            <CheckBox x:Name="_groupEnabled" Click="GroupEnabled_Click" IsChecked="{Binding Path=Checked}">
                                <TextBlock x:Name="_groupName" Text="{Binding Path=Group.Name}" ></TextBlock>
                            </CheckBox>
                            <Button x:Name="_deleteGroup" FontStyle="Normal" Click="DeleteGroup_Click" />
                        </WrapPanel>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    

    对于少量的组,所有内容都显示在一行上。

    组+组1组2组3

    当我添加更多组时,包含项目的“包装”面板将包装,但用于添加新组的初始标签和按钮位于其自己的行上,数据绑定组从下一行开始。


    第1组第2组第3组第4组

    组+组1组2组3
    第4组第5组

    1 回复  |  直到 15 年前
        1
  •  1
  •   Tim Cooper    13 年前

    组+组1组2组3
    第4组第5组

    Grid 和一个内在的 WrapPanel . 否则,您可以选择:

    1. 包括其他控件( Label Button )或者是一个视图模型,它表示要绑定到的集合中的那些控件。
    2. 写你自己的 Panel .
    推荐文章