0

I have a requirement in my program that the object bound (from ViewModel) in a Combobox is updated as soon as an item is selected in the combobox. Currently, the object only updates once the edit is committed by either pressing Enter or leaving the cell. The user does not want the extra step.

My thought would be to have the act of selecting an item in the combobox trigger the CommitEdit() method and then CancelEdit(). However, I cannot seem to find a way to hook into the SelectionChanged event for the DataGridComboBoxColumn as it is not available.

Other suggestions have been to listen in the viewmodel for a property change event but the property is not changed until the Cell Edit is finished.

Can anyone think of a way to cause the selection of a new item (index) in a DataGridCombobox to close the edit of the cell as if the user pressed Enter or left the cell?

NOTE: I cannot use .NET 4.5 due to customer limitations.

Josh
  • 101
  • 1
  • 4

1 Answers1

1

I've had similar issue but i just found out the solution using attached property, This may not exactly fix your problem but it will help in datagrid selection changed issue.

Below is the attached property and handler methods

public static readonly DependencyProperty ComboBoxSelectionChangedProperty = DependencyProperty.RegisterAttached("ComboBoxSelectionChangedCommand",
                                                                                                              typeof(ICommand),
                                                                                                              typeof(SpDataGrid),
                                                                                                              new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridEvent)));


public static void AttachOrRemoveDataGridEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
  DataGrid dataGrid = obj as DataGrid;
  if (dataGrid != null)
  {   
      if (args.Property == ComboBoxSelectionChangedProperty)
      {
        dataGrid.SelectionChanged += OnComboBoxSelectionChanged;
      }
    }
    else if (args.OldValue != null && args.NewValue == null)
    { if (args.Property == ComboBoxSelectionChangedProperty)
      {
        dataGrid.SelectionChanged -= OnComboBoxSelectionChanged;
      }
}
}

private static void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
  DependencyObject obj = sender as DependencyObject;
  ICommand cmd = (ICommand)obj.GetValue(ComboBoxSelectionChangedProperty);
  DataGrid grid = sender as DataGrid;

  if (args.OriginalSource is ComboBox)
  {
    if (grid.CurrentCell.Item != DependencyProperty.UnsetValue)
    {
      //grid.CommitEdit(DataGridEditingUnit.Row, true);
      ExecuteCommand(cmd, grid.CurrentCell.Item);
    }
  }
}

SpDataGrid is the custom control that i inherited from data grid.

I added below style in generic.xaml as i use the resourcedictionary for style (you can certainly add inside the datagrid).

<Style TargetType="{x:Type Custom:SpDataGrid}">
     <Setter Property="Custom:SpDataGrid.ComboBoxSelectionChangedCommand" Value="{Binding ComboBoxSelectionChanged}"/>
 </Style>

ComboBoxSelectionChanged is the command in my viewmodel. OnComboBoxSelectionChanged i commented the commitedit because in my case the values were already updated.

Let me know if anything is not clear or any questions. Hope this helps.

Kathirk
  • 81
  • 2