What event handler to use for ComboBox Item Selected (Selected Item not necessarily changed)

后端 未结 12 1586
囚心锁ツ
囚心锁ツ 2020-12-13 08:48

Goal: issue an event when items in a combobox drop down list is selected.

Problem: Using \"SelectionChanged\", however, if the user

12条回答
  •  不知归路
    2020-12-13 09:14

    This is a DependencyObject for attaching to a ComboBox.

    It records the currently selected item when the dropdown is opened, and then fires SelectionChanged event if the same index is still selected when the dropdown is closed. It may need to be modified for it to work with Keyboard selection.

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Web.UI.WebControls;
    
    namespace MyNamespace 
    {
        public class ComboAlwaysFireSelection : DependencyObject
        {
            public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached(
                "Active",
                typeof(bool),
                typeof(ComboAlwaysFireSelection),
                new PropertyMetadata(false, ActivePropertyChanged));
    
            private static void ActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var element = d as ComboBox;
                if (element == null) 
                    return;
    
                if ((e.NewValue as bool?).GetValueOrDefault(false))
                {
                    element.DropDownClosed += ElementOnDropDownClosed;
                    element.DropDownOpened += ElementOnDropDownOpened;
                }
                else
                {
                    element.DropDownClosed -= ElementOnDropDownClosed;
                    element.DropDownOpened -= ElementOnDropDownOpened;
                }
            }
    
            private static void ElementOnDropDownOpened(object sender, EventArgs eventArgs)
            {
                _selectedIndex = ((ComboBox) sender).SelectedIndex;
            }
    
            private static int _selectedIndex;
    
            private static void ElementOnDropDownClosed(object sender, EventArgs eventArgs)
            {
                var comboBox = ((ComboBox) sender);
                if (comboBox.SelectedIndex == _selectedIndex)
                {
                    comboBox.RaiseEvent(new SelectionChangedEventArgs(Selector.SelectionChangedEvent, new ListItemCollection(), new ListItemCollection()));
                }
            }
    
            [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
            [AttachedPropertyBrowsableForType(typeof(ComboBox))]
            public static bool GetActive(DependencyObject @object)
            {
                return (bool)@object.GetValue(ActiveProperty);
            }
    
            public static void SetActive(DependencyObject @object, bool value)
            {
                @object.SetValue(ActiveProperty, value);
            }
        }
    }
    

    and add your namespace prefix to make it accessible.

    
    

    and then you need to attach it like so

    
    

提交回复
热议问题