【问题标题】:Add more of my usercontrol container or usercontrol collection display添加更多我的用户控件容器或用户控件集合显示
【发布时间】:2012-01-03 02:38:57
【问题描述】:

我不知道我正在寻找的容器/控件会被称为什么,所以我无法真正搜索它。

添加更多相同的Usercontrol用户控件

  • 点击 + 会在现有控件的右侧添加一个新的 My Usercontrol 实例
  • 点击X会释放被点击的用户控件
  • 我并不是真的在寻找可以将每个新实例放在新选项卡上的选项卡控件,但如果没有其他内容,它可能会这样做。
  • 设计显然不是如图所示,图像只是说明基本思想

任何关键字/名称建议或现有实现的链接?

例如也许有一种风格可以将 ListBox 变成合适的东西?

【问题讨论】:

    标签: c# .net wpf layout user-controls


    【解决方案1】:

    我会使用 ItemsControl 并将其自定义为 ItemsPanelTemplate 以成为您想要的任何东西。

    ItemsControls 用于遍历对象集合,并以您想要的任何形式显示它们。我写了一些简单的代码示例 here 如果你有兴趣,或者这里是另一个简单的例子:

    <DockPanel x:Name="RootPanel">
        <Button Style="{StaticResource AddButtonStyle}"
                DockPanel.Dock="Right" VerticalAlignment="Center"
                Command="{Binding AddItemCommand" />
    
        <ScrollViewer>
            <ItemsControl ItemsSource="{Binding MyCollection}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
    
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <local:MyUserControl />
    
                            <Button Style="{StaticResource RemoveButtonStyle}"
                                Command="{Binding ElementName=RootPanel, Path=DataContext.RemoveItemCommand}"
                                CommandParameter="{Binding }"
                                HorizontalAlignment="Left" VerticalAlignment="Top" />
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </DockPanel>
    

    您的 ItemsControl 将绑定到 ObservableCollection 对象,您的添加/删除按钮将简单地从该集合中添加/删除项目。因为它是ObservableCollection,所以它会在集合发生变化并自动更新时通知 UI。

    【讨论】:

      【解决方案2】:

      您确实可以使用 ListBox 并设置它的 ItemTemplate 和 ItemsPanelTemplate:

      <ListBox>
          <ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                  <StackPanel Orientation="Horizontal"></StackPanel>
              </ItemsPanelTemplate>
          </ListBox.ItemsPanel>
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <Label Content="{Binding Name}" />
              </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>
      

      当然,您的 ItemTemplate 将是对您的控件的引用。

      【讨论】:

      • 谢谢!我还有一个问题,对 53AN 的第一条评论?
      【解决方案3】:

      您可以查看称为轮播控件的东西,它使用其背后的对象列表并以类似于 iTunes 的方式显示它们。这可能有点过头了,但这是一种解决方案。一个例子可以看here

      如果这对于您的需求来说太高级了,是否可以像一个带有滚动条的堆栈面板一样简单,该滚动条绑定到您的用户控件列表?

      【讨论】:

      • 旋转木马看起来很甜!为了使它满足我的需求,1)我怎样才能让每个项目都有一个 X 单击会删除该项目,以及 2)我是否总是将一个项目添加到列表的末尾,这将是 + usercontrol ,点击哪个会添加一个新项目?
      • 在每个项目的模板中,您可以添加一个按钮,单击该按钮将从绑定到轮播的列表中删除所选项目。这反过来会从轮播中删除该项目。我没有使用链接文章中的轮播控件。这只是我在寻找时发现的最完整的例子。我想在列表中添加一个项目会将其添加到轮播列表的末尾。
      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多