0

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

mechbaral
  • 103
  • 7

4 Answers4

1

I have tried it and the methods are called. The problem is probably that you use the wrong property to retrieve the selected item. Try this instead:

SelectedComboBoxItem = this.MyComboBox.SelectedItem as string;
mami
  • 586
  • 1
  • 4
  • 14
  • I am sorry, it seems my question was not clear but the problem i am trying to solve is that...from this statement `this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged1);` from what i believe is the event SelectionChanged should call the method tiedup to it but it doesnot. In fact I tried using breakpoint in this statement and check if the event fires if i change the item in combobox but it doesnot. I will also try as you suggested – mechbaral Nov 28 '18 at 12:10
  • I have tried your code and the events are called. `SelectionChanged` first, `DropDownClosed` second. I've suggested to use `SelectedItem` for SelectionChanged, because this event is called before setting Text. For DropDownClose, it is ok to use Text because it is set by the time this event handler is called. – mami Nov 28 '18 at 12:24
1

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
}
Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • I am sorry, it seems my question was not clear but the problem i am trying to solve is that...from this statement `this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged1);` from what i believe is the event SelectionChanged should call the method tiedup to it but it doesnot. In fact I tried using breakpoint in this statement and check if the event fires if i change the item in combobox but it doesnot. I will also try as you suggested – mechbaral Nov 28 '18 at 12:10
1

You need to do 2 things.

  1. You should implement INotifyPropertyChanged Interface with a backing field to your property SelectedComboBoxItem.

  2. You need to set DataContext to your class like this.

    this.DataContext = this;

Mathivanan KP
  • 1,979
  • 16
  • 25
  • I am sorry, it seems my question was not clear but the problem i am trying to solve is that...from this statement `this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged1);` from what i believe is the event SelectionChanged should call the method tiedup to it but it doesnot. In fact I tried using breakpoint in this statement and check if the event fires if i change the item in combobox but it doesnot. I will also try as you suggested – mechbaral Nov 28 '18 at 12:11
0

Bellow is the answer and the working code just in case if someone needs it

MainWindow.xaml.cs

using System.ComponentModel;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Diagnostics;

namespace Combobox
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<string> NameOfPerson { get; set; }
        private string _SelectedComboBoxItem;
        public string SelectedComboBoxItem
        {
            get
            {
                return _SelectedComboBoxItem;
            }

            set
            {
                if (_SelectedComboBoxItem == value)
                    return;
                _SelectedComboBoxItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedComboBoxItem)));
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            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 += OnMyComboBoxChanged1;
            this.MyComboBox.DropDownClosed += OnMyComboBoxChanged2;
        }


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

    }

}

XAML

<Window x:Class="Combobox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Combobox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="300">
    <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>
</Window>
mechbaral
  • 103
  • 7