我正在尝试将列表框的 itemsSource 设置为复合集合。
<ListBox x:Name="moduleMenu" SelectedItem="{Binding SelectedSourceViewDetail}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ListBox.ItemsSource>
<CompositeCollection>
<ListBoxItem DataContext="{Binding SourceModule.Settings}" Content="{Binding Name}"/>
<CollectionContainer Collection="{Binding SourceModule.ExtraViews}" />
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>
但是与 selectedItem 的绑定(bind)不起作用。选择项目后,如何在 View 模型中获取所选项目?
最佳答案
而不是像在 xaml 中那样设置 ItemsSource。我建议您使用返回 CompositeCollection 并将该集合绑定(bind)到 ItemsSource 的转换器。 CompositeCollection 将是 Settings 和 ExtraViews 的集合
您的绑定(bind)看起来像 ItemsSource = "{Binding SourceModel, Converter={StaticResource myConverter}}"
.在转换器的 Convert 方法中,从 sourcemodel.Settings 和 sourcemodel.ExtraViews 创建复合集合并返回。
谢谢
关于.net - 在wpf中使用复合集合获取选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531697/