简体   繁体   English

如何从 DataGrid 获取 ComboBox 值

[英]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.当我在我的数据网格中编辑一个单元格时,会引发 combobox 并且我从所述 combobox 中选择一个值,并且当编辑结束时触发 CellEditEnding 事件并且我想获得我选择的值但我无法找到我的 ZA828BD64521683DEC3542 值在我的事件中。

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 ?您是否有理由不使用DataGridComboBoxColumn

Well using MVVM i would call an ICommand on my ViewModel where i can simply access the bounded value of the ComboBox .好吧,使用 MVVM 我会在我的 ViewModel 上调用ICommand ,我可以在其中简单地访问ComboBox的有界值。 You can bind Commands using EventTrigger .您可以使用EventTrigger绑定命令。

First add the namespace:首先添加命名空间:

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

Setting the Interaction.Triggers :设置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}"关于绑定的一些建议,我不确定ComboBox是否需要这样做,但在TextBox上要获得更新的值,您必须将UpdateSourceTrigger设置为PropertyChanged ,如下所示: Text="{Binding text, UpdateSourceTrigger=PropertyChanged}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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