11

I have a datagrid, with a combobox column

<DataGridComboBoxColumn x:Name="DataGridComboBoxColumnBracketType" Width="70" Header="Tipo di staffa" SelectedValueBinding="{Binding type, UpdateSourceTrigger=PropertyChanged}">                    
            </DataGridComboBoxColumn>

I want an event that is fired only when the user changes the value into the combobox. How can I resolve this?

4
  • 1
    You need to change it back to how you previously had it with the Selector.Selected and using the grid. In order to access the ComboBoxItem I believe you would have to drill down into the ComboBox itself.
    – JLott
    Oct 11, 2013 at 16:24
  • 1
    What's the problem? Is it just that the handler doesn't get invoked? I wonder about the scope -- maybe the style has to be defined in a root resource dictionary.
    – McGarnagle
    Oct 11, 2013 at 17:12
  • 1
    Why can't you just subscribe to PropertyChanged on type in viewmodel??
    – Maverik
    Oct 14, 2013 at 8:28
  • 1
    @Maverik do you mean that I need to manage this issue into the set of type property? Oct 14, 2013 at 8:34

4 Answers 4

20

I found a solution to this on CodePlex. Here it is, with some modifications:

<DataGridComboBoxColumn x:Name="Whatever">                    
     <DataGridComboBoxColumn.EditingElementStyle>
          <Style TargetType="{x:Type ComboBox}">
               <EventSetter Event="SelectionChanged" Handler="SomeSelectionChanged" />
          </Style>
     </DataGridComboBoxColumn.EditingElementStyle>           
</DataGridComboBoxColumn>

and in the code-behind:

private void SomeSelectionChanged(object sender, SelectionChangedEventArgs e)
{
     var comboBox = sender as ComboBox;
     var selectedItem = this.GridName.CurrentItem;

}
1
  • This does not work for routed events like DropDownClosed
    – Matthew S
    Aug 6, 2018 at 22:01
6

And the xaml code provided by @kevinpo from CodePlex and help from David Mohundro's blog, programatically:

var style = new Style(typeof(ComboBox));
style.Setters.Add(new EventSetter(ComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(SomeSelectionChanged)));
dataGridComboBoxColumn.EditingElementStyle = style;
4

To Complete Kevinpo answer, for the code behind you should add some protection because the selectionChanged event is triggered 2 time with a datagridcolumncombobox:

1) first trigger : when you selected a new item

2) Second trigger : when you click on an other datagridcolumn after you selected a new item

The problem is that on the second trigger the ComboBox value is null because you don't have changed the selected item.

private void SomeSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as ComboBox;
    if (comboBox.SelectedItem != null)
    {
        YOUR CODE HERE
    }
}

That was my problem, I wish it will help someone else !

1

The problem of getting SelectionChanged events to fire on DataGridComboBoxColumn cells was one that plagued me recently. I used the following solution:

 private void DataGridView_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
    {
        if (e.Column.DisplayIndex == 3) //Use this IF statement to specifiy which combobox columns you want to attach the event to.
        {
            ComboBox? cb = e.EditingElement as ComboBox;
            if (cb != null)
            {
                // As this event fires everytime the user starts editing the cell you need to dettach any previously attached instances of "My_SelectionChanged", otherwise you'll have it firing multiple times.
                cb.SelectionChanged -= My_SelectionChanged;
                // now re-attach the event handler.
                cb.SelectionChanged += My_SelectionChanged;
            }
        }
    }

Then set up your custom SelectionChanged event handler to whatever you need:

private void My_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Your event code here...
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.