wpfeventscomboboxselectionchanged

WPF ComboBox SelectionChanged and DropDownClosed events is not working


I am trying to call method by linking it to the Selection changed event and DropDownClosed event of the Combobox in WPF but when i change the item in combobox it is not calling the function it suppose to (in my case OnMyComboBoxChanged1 and OnMyComboBoxChanged2).

MainWindow.xaml.cs

 public partial class MainWindow : Window
    {
        public List<string > NameOfPerson { get; set; }
        public string SelectedComboBoxItem { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            NameOfPerson = new List<string>();
            NameOfPerson.Add("Ram");
            NameOfPerson.Add("Sita");
            NameOfPerson.Add("Hari");
            NameOfPerson.Add("Kumar");
            NameOfPerson.Add("Jay");
            NameOfPerson.Add("Bikash");
            MyComboBox.ItemsSource = NameOfPerson;
           this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged1);
           this.MyComboBox.DropDownClosed += new System.EventHandler(OnMyComboBoxChanged2);
        }

        private void OnMyComboBoxChanged1(object sender, SelectionChangedEventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.Text;
        }
        private void OnMyComboBoxChanged2(object sender, System.EventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.Text;
        }

    }

XAML

 <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <Label Content="Combobox"/>
            <ComboBox x:Name="MyComboBox" Margin="50,0,0,0" Width="80"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
            <Label Content="The selected item is : "/>
            <Label Content="{Binding SelectedComboBoxItem}"/> 
        </StackPanel>
       
    </StackPanel>

Thank you for the Help


Solution

  • The content of the label won't update because nothing is telling it to update - there is no automatic notification for standard C# properties.

    You need to implement INotifyPropertyChanged for your SelectedComboBoxItem property, or even better switch to the MVVM design pattern.

    The alternative is to use direct data binding

    <Label Content="{Binding ElementName="MyComboBox", Path=SelectedItem}" />
    

    This works because properties of controls are (usually) DependencyProperties which do provide notification of changes.

    Edit after comment

    Please post a minimal, complete, and verifiable example then ... the following code works fine for me.

    public MainWindow()
    {
        InitializeComponent();
    
        var NameOfPerson = new List<string>();
        NameOfPerson.Add("Ram");
        NameOfPerson.Add("Sita");
        NameOfPerson.Add("Hari");
        NameOfPerson.Add("Kumar");
        NameOfPerson.Add("Jay");
        NameOfPerson.Add("Bikash");
        MyComboBox.ItemsSource = NameOfPerson;
    
        MyComboBox.SelectionChanged += (s,e) => MyComboBoxOnSelectionChanged();
    }
    
    private void MyComboBoxOnSelectionChanged()
    {
        SelectedComboBoxItem = MyComboBox.SelectedItem.ToString();
        Debugger.Break(); // proof that the event handler is being called
    }