我编写了一个 UserControl 来显示自定义对象的一些属性。这些对象有多个实例,所以我有一个 ObservableCollection,这样我就可以将它们设置为绑定(bind)到 ListView 的 ItemsSource。现在我可以为我的 ListView 中的类的每个实例显示此 UserControl 的实例。
问题是我真的不想要 ListView 的行为。我不希望用户能够选择整个 UserControl。事实上,用户应该能够选择 UserControl 中的各个元素。
我想过只使用 StackPanel 来放入这些 UserControl,但它没有 ItemesSource 属性。有没有一种简单的方法可以做到这一点?
最佳答案
用 ItemsControl
替换您的 ListView
并将 ItemTemplate
设置为适合您的对象的 DataTemplate
。如果您想更改面板的项目布局方式,您可以设置 ItemsPanel
。
<ItemsControl ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource ItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
查看此 example
关于c# - 选择正确的 ItemsSource 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5032055/