.net - 在wpf中使用复合集合获取选定项

标签 .net wpf mvvm binding compositecollection

我正在尝试将列表框的 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/

相关文章:

c# - 我实际上如何从方法外部获得异步 Web 请求的响应?

c# - 带有 LinqToSQL 的 MVVM

c# - 为什么有时在计时器滴答事件中它会连续调用该方法两次?

c# - 如何在没有段落格式的情况下将生成的 HTML 字符串输出到文本框

c# - 为什么 'unbox.any' 不像 'castclass' 那样提供有用的异常文本?

c# - 在 UserControl 中居中标签

c# - WPF:MVVM 中的 TreeView

android - 数据绑定(bind)错误 : databinding does not exist

c# - MsiQueryProductState如何判断安装了VC++

c# - 有没有办法在字符串上使用 less than ?