c# - WPF如何将ComboBoxes选定项目用作xctk :PropertyGrid's SelectedObject in MVVM

标签 c# wpf mvvm combobox propertygrid

我有一个组合框,其中包含不同类型的项目,因此我无法绑定(bind)到列表。我已经单独设置了 xaml 中的项目,因为只有两个项目,所以在这种情况下不用担心维护开销。
我还有一个属性网格(来自 Xceed),并且想将它的 selectedObject 设置为我的组合框的选定项。我正在使用 MVVM 模式,但找不到在 xaml 中设置属性网格选定对象的解决方案。
这是组合框和属性网格的 xaml,没什么可看的,只是每个的声明。

<ComboBox Grid.Row="0" Name="TestComboBox" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto">
    <ComboBoxItem <!-- Set the item binding for item 1 to property in view model-->>Item 1</ComboBoxItem>
    <ComboBoxItem <!-- Set the item binding for item 2 to property in view model-->>Item 2</ComboBoxItem>
<xctk:PropertyGrid Grid.Row="1" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" SelectedObject="{Binding <!-- Bind to comboBoxes selected item-->}"/>

我不确定这是否可行,因为我认为 ComboBox 使用对象列表作为其结构,因此它可能无法正常工作,根据需要单独设置项目。

最佳答案

您可以绑定(bind)SelectedObject ComboBox 中当前选定项的属性像这样:

<xctk:PropertyGrid ... SelectedObject="{Binding SelectedItem, ElementName=TestComboBox}">

您可能不想添加 ComboBoxItemsComboBox但在您的 XAML 标记中,因为这样您将绑定(bind)到 ComboBoxItem ,但这是另一个故事。

关于c# - WPF如何将ComboBoxes选定项目用作xctk :PropertyGrid's SelectedObject in MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201583/

相关文章:

c# - 无法显示链接字段值表+ReportViewer

c# - Linq C# 基于变量的查询

c# - 在 C# 中高效查找二维数组 T[][] 中的唯一元素

c# - ScrollViewer 上的 DataGrids 阻止它滚动

swift - Swift Bond 中 model 和 viewModel 的绑定(bind)

具有许多其他 View 模型依赖项的 Wpf 主视图模型

c# - 如何使用转换从 web.config 中删除元素?

c# - 带有两侧的 WPF 按钮

c# - 我们可以在 Mac OS X 上运行 C#/WPF 应用程序吗?

.net - 使用绑定(bind)数据和转换器对 datagrid 列进行排序