Adding new element to a ComboBox which is in a DataGrid in WPF using MVVM

93 views Asked by At

I have a DataGrid which contains Transactions. I have an InterestOrDividend column where I can select a value using a ComboBox. This works fine.

A new feature would be to enter a value and add it to the list of possibilities. I set IsEditable to true and added Interaction.Triggers from http://schemas.microsoft.com/expression/2010/interactivity

Problem 1: It seems InterestOrDividendSelectionChangedCommand not only fires when the selection is changed but also when I scroll the DataGrid and such rows come into view which has a not null value in the InterestOrDividend column. Moreover, when a new value is entered (which is not in the list), the event does not fire.

Problem 2: I want to bind the Text property of the ComboBox to get the newly added value. It seems the event fire before the Text property changes, so I get the old value.

<DataGridTemplateColumn Header="{x:Static r:Resource.InterestOrDividend}"
   CellTemplate="{StaticResource InterestOrDividendEditingTemplate}"
   CellEditingTemplate="{StaticResource InterestOrDividendEditingTemplate}" />

<DataTemplate x:Key="InterestOrDividendEditingTemplate">
        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                            AncestorType={x:Type DataGrid}}, Path=DataContext.AppData.AlienTypeObjects}" 
                  SelectedItem="{Binding InterestOrDividend}" 
                  DisplayMemberPath="FullName" 
                  IsEditable="True" 
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type DataGrid}}, 
                        Path=DataContext.InterestOrDividendSelectionChangedCommand}"
                        CommandParameter="{Binding RelativeSource={RelativeSource  FindAncestor, 
                        AncestorType={x:Type ComboBox}}, Path=Text}"
                                           />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
</DataTemplate>
1

There are 1 answers

1
Istvan Heckl On BEST ANSWER

This is my solution. Instead of using the EventTrigger I catch the new element in the setter of NewInterestOrDividend. It is important that the UpdateSourceTrigger is LostFocus. When InterestOrDividend is null and you change the focus then value in NewInterestOrDividend contains the new value.

    <DataTemplate x:Key="InterestOrDividendEditingTemplate">
        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                            AncestorType={x:Type DataGrid}}, Path=DataContext.AppData.AlienTypeObjects}" 
                  DisplayMemberPath="FullName" Style="{StaticResource ComboBoxError}" 
                  IsEditable="True" 
                  SelectedItem="{Binding InterestOrDividend, UpdateSourceTrigger=PropertyChanged}" 
                  Text="{Binding NewInterestOrDividend, UpdateSourceTrigger=LostFocus}">
        </ComboBox>
    </DataTemplate>