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