【问题标题】:In WPF set selected column value from Datagrid to Combobox (display section)在 WPF 中,将 Datagrid 中的选定列值设置为 Combobox(显示部分)
【发布时间】:2016-03-26 15:15:35
【问题描述】:

我有DataGridEquipments。而且我还有EquipmnetMakers 类。

现在我制作了“编辑”按钮,它可以编辑所有值。 我使用ItemsSource 用所有可用的EquipmentMakers 填充Combobox

现在我想在Combobox 中显示选定行的EquipmentMaker

请帮忙!

【问题讨论】:

  • 请移除“entity-framework”和“ef-code-first”标签。您的问题与这些无关。

标签: wpf entity-framework combobox datagrid ef-code-first


【解决方案1】:

我认为有几种可能。

其中一个正在使用DataGridComboBoxColumnSelectedItemBinding 属性。 它必须指向EquipmentMaker 属性(我想你的Equipment 类有一个EquipmentMaker 属性)。


<DataGrid 
    ItemsSource="{Binding EquipmentMakers}" AutoGenerateColumns="False" 
    >           
    <DataGrid.Columns>
        <!-- Other columns here -->
        <DataGridComboBoxColumn Header="Equipment makers"  SelectedItemBinding="{Binding EquipmentMaker}">
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="IsDropDownOpen" Value="True" />
                    <Setter Property="ItemsSource" 
                            Value="{Binding Path=DataContext.AllEquipmentMakers, RelativeSource={RelativeSource AncestorType={x:Type Window}},
                        UpdateSourceTrigger=PropertyChanged}" />
                    <Setter Property="IsReadOnly" Value="True"/>
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="ItemsSource" 
                            Value="{Binding Path=DataContext.AllEquipmentMakers, RelativeSource={RelativeSource AncestorType={x:Type Window}},
                        UpdateSourceTrigger=PropertyChanged, IsAsync=True}" />
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>


此示例需要一个具有 Equipments 集合和列表 AllEquipmentMakers 的视图模型,其中包含用户可以在组合框中选择的所有 EquipmentMaker 实例。

您也可以查看this 问题。这里使用了SelectedValueBinding

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多