سؤال

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());


        }
    }
هل كانت مفيدة؟

المحلول

Why not?

ComboBox myCombobox = (ComboBox)sender;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top