简体   繁体   中英

How to get ComboBox value from DataGrid

When I edit a cell in my datagrid a combobox is raised and I choose a value from said combobox, and when the edit ends a CellEditEnding event is triggered and I want to get the value I chose but I am not able to locate my combobox value in my event.

How can I get the value?

<DataGrid x:Name="ModulesTable" 
              Background="#d8deff" 
              Foreground="#383e42" 
              Grid.Row="1" 
              SelectionChanged="ModulesTable_SelectionChanged" 
              CellEditEnding="ModulesTable_CellEditEnding" 
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              CanUserReorderColumns="False"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="ModuleType" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=ModuleTypeName}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Text="{Binding Path=ModuleTypeName}" SelectionChanged="ComboBox_SelectionChanged" IsEditable="True">
                            <ComboBoxItem>IZMO</ComboBoxItem>
                            <ComboBoxItem>LIMOAP</ComboBoxItem>
                            <ComboBoxItem>LIMOKO</ComboBoxItem>
                            <ComboBoxItem>VIMO</ComboBoxItem>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

This is what I have so far:

    private void ModulesTable_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        FrameworkElement element1 = ModulesTable.Columns[1].GetCellContent(e.Row);
        Log.Debug("TypeOF: " + element1.GetType()); // Type is ContentPresenter
        if (element1.GetType() == typeof(ComboBox)) // This doesnt work because its not ComboBox
        {
            var value = ((ComboBox)element1).Text;
            // Get value somehow??
        }

    }

Is there a reason why you don't use a DataGridComboBoxColumn ?

Well using MVVM i would call an ICommand on my ViewModel where i can simply access the bounded value of the ComboBox . You can bind Commands using EventTrigger .

First add the namespace:

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Setting the Interaction.Triggers :

<DataGrid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="CellEditEnding">
                <i:InvokeCommandAction Command="{Binding CellEditEndCommand, Mode=OneWay}"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="RowEditEnding">
                <i:InvokeCommandAction Command="{Binding CellEditEndCommand, Mode=OneWay}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>

Some advice for the binding, i'm not sure if if this is needed for ComboBox but on TextBox to get the updated value you have to set the UpdateSourceTrigger to PropertyChanged like this: Text="{Binding text, UpdateSourceTrigger=PropertyChanged}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM