C# - how do I prevent mousewheel-scrolling in my combobox?

前端 未结 4 1847
小蘑菇
小蘑菇 2020-12-01 03:18

I have a combobox and I want to prevent the user from scrolling through the items with the mousewheel.

Is there an easy way to do that?

(C#, VS2008)

4条回答
  •  孤街浪徒
    2020-12-01 04:02

    My Combobox's were placed inside a DataGrid [C#, WPF XAML] just like this:

        
            
                ...
                
                    
                        
                            
                        
                    
                
                ...
            
        
    

    So whenever a DropDown was closed after selecting a Value, the Mousewheel would scroll that Combobox's Items and modify my Selection.

    I ended up modifying my XAML to look like this:

        
            
                
            
            
                ...
                
                    
                        
                            
                        
                    
                
                ...
            
        
    

    And adding these lines in codebehind

            public void dgvCombobox_Loaded(Object sender, RoutedEventArgs e)
            {
                ((ComboBox)sender).DropDownClosed -= ComboBox_OnDropDownClosed;
                ((ComboBox)sender).DropDownClosed += new System.EventHandler(ComboBox_OnDropDownClosed);
            }
    
            void ComboBox_OnDropDownClosed(object sender, System.EventArgs e)
            {
                dgvFieldsMapping.Focus();
            }
    

    In this way I just move the Focus away from the ComboBox to the outer DataGrid after closing its corresponding DropDown, so I don't need to add any dummy FrameWorkElement

提交回复
热议问题