【问题标题】:Picking the right ItemsSource container选择正确的 ItemsSource 容器
【发布时间】:2011-02-17 17:02:10
【问题描述】:

我编写了一个 UserControl,用于显示自定义对象的一些属性。这些对象有多个实例,因此我有它们的 ObservableCollection,因此我可以将它们设置为绑定到 ListView 的 ItemsSource。现在我可以在我的 ListView 中为我的类的每个实例显示这个 UserControl 的实例。

问题是我真的不想要 ListView 的行为。我不希望用户能够选择整个 UserControl。事实上,用户应该能够选择 UserControl 中的各个元素。

我曾想过只使用 StackPanel 来放入这些 UserControl,但它没有 ItemesSource 属性。有没有简单的方法来实现这一点?

【问题讨论】:

    标签: c# wpf xaml itemssource


    【解决方案1】:

    将您的ListView 替换为ItemsControl,并将ItemTemplate 设置为适合您的对象的DataTemplate。如果您想更改面板的项目布局方式,可以设置ItemsPanel

    <ItemsControl ItemsSource="{Binding Items}"
                  ItemTemplate="{StaticResource ItemTemplate}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    看到这个example

    【讨论】:

    • 优秀。我必须在 ItemsControl 模板中添加一个 ScrollViewer,但这没什么大不了的。除此之外,我只能用 ItemsControl 替换 ListView。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-03-18
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多