0

I have DataGrid with Equipments in it. And also I have EquipmnetMakers class.

Now I made "Edit" button, which can edit all values. I use ItemsSource to fill Combobox with all available EquipmentMakers.

Now I want to display selected row's EquipmentMaker in Combobox.

Please Help!

panda007
  • 17
  • 1
  • 8
  • Please remove the 'entity-framework' and the 'ef-code-first' tags. Your question is not related to those. – Martin Mar 30 '16 at 13:44

1 Answers1

0

I think there are several possibilities.

One of them is using the SelectedItemBinding property of the DataGridComboBoxColumn. It must point to the EquipmentMaker property (I suppose your Equipment class has an EquipmentMaker property).


<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>


This example expects a view model with an Equipments collection and a list AllEquipmentMakers which holds all instances of EquipmentMaker the user may choose in the combo box.

You may also check out this question. Here a SelectedValueBinding is used.

Community
  • 1
  • 1
Martin
  • 5,165
  • 1
  • 37
  • 50