Search code examples
wpfdatagridcomboboxcolumn

WPF DataGridComboBoxColumn DropdownClosed events


I am quite new to the WPF....I ran into this issue where it says "The dropdownclosed is not a routedevent". here is my code:

<DataGridComboBoxColumn x:Name="Fleet_Combo" Header="Fleet" Width = "30*" ItemsSource="{Binding acTypeFleet}" SelectedItemBinding="{Binding Fleet,Mode=TwoWay}">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <EventSetter Event="SelectionChanged" Handler="FleetComboBox_SelectionChanged"/>
                            <EventSetter Event="DropDownClosed" Handler="ComboBox_DropDownClosed"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>                        
                </DataGridComboBoxColumn>

Please help, thank you.


Solution

  • As the error message says: DropDownClosed isn't a RoutedEvent, so you can't create a style for ComboBoxes and have them all inherit the event via an EventSetter.

    A workaround to invoke the event is to use an event that is a RoutedEvent, and hook into that appropriately. A suitable candidate is Loaded. Follow Alain's answer here to get the Loaded event:

    <Style x:Key="ComboBoxCellStyle" TargetType="ComboBox">
      <EventSetter Event="Loaded" Handler="ComboBox_Loaded" />
    </Style>
    

    From the loaded event, you can get to the DropDownClosed event

    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
      ((ComboBox)sender).DropDownClosed -= ComboBox_OnDropDownClosed;
      ((ComboBox)sender).DropDownClosed += new 
         System.EventHandler(ComboBox_OnDropDownClosed);
    }
    

    and from there call the appropriate handler:

    void ComboBox_OnDropDownClosed(object sender, System.EventArgs e)
    {
      ...
    }