0

My Binding for the combobox in datagridtemplatecolumn is working fine but I am not able to access the selected value of the combobox from code behind on the selection changed event. I suppose there is some problem with ContentPresenter which is not getting mapped to combobox. Here is my XAML Code:

<DataGridTemplateColumn Header="CSV/Excel Column">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cmbExcelColumn"
                            ItemsSource="{Binding ExcelColumn}"
                            Width="220"
                                      SelectedValuePath="SelectedValue"
                                      SelectionChanged="cmbExcelColumn_SelectionChanged"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Here is my code behind to access selected value of combobox:

private void cmbExcelColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        List<string> selectedIncrememntors = new List<string>();
        dgMappingColumns.UpdateLayout();
        for (int i = 0; i < dgMappingColumns.Items.Count; i++)
        {

            ComboBox myCombobox = dgMappingColumns.Columns[1].GetCellContent(dgMappingColumns.Items[i]) as ComboBox;

            if (myCombobox.SelectedValue != null)
                selectedIncrememntors.Add(myCombobox.SelectedValue.ToString());


        }
    }
Afaq
  • 1,146
  • 1
  • 13
  • 25

1 Answers1

1

Why not?

ComboBox myCombobox = (ComboBox)sender;
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Thanks. Your code is helping to gohead. How to bind source like this .'cmbExcelColumn.DisplayMemberPath = Datatble1.Columns["SHORTNAME"].ToString();' here cmbExcelColumn is combo box. I am need to do in CodeBehind only. Please Suggest me on this one. – Rajesh D Feb 15 '16 at 12:00