How to get SelectedItem in the ComboBox?

Emon Haque 3,176 Reputation points
2020-10-31T02:55:21.097+00:00

I was experimenting on one of my Custom Controls, PathButton which is an extended Button. All that I've for that PathButton is a Style, Command and in some cases CommandParameter. So when I click that, it executes the Command and it also raises the Click event. Since I don't have any use of that Event, I've overridden the OnClick like this:

protected override void OnClick() => Command.Execute(CommandParameter);

and it does what I want it to do BUT doesn't raise the Click event, very nice. I've another Custom Control, Combo which is an extended ComboBox, and it raises SelectionChanged whenever I change selection. So to get rid of that event I've to do something here:

protected override void OnSelectionChanged(SelectionChangedEventArgs e) { }

if I call base.OnSelectionChanged(e), it works as expected BUT raises that event. If I remove the base call and keep it blank, it doesn't update the content in the Box. What to have there to update the content of the box and suppress the event?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,755 questions
{count} votes

Accepted answer
  1. Mia Wu-MSFT 327 Reputation points Microsoft Employee
    2020-11-05T02:54:02.837+00:00

    If you don't want to trigger SelectionChanged Event but still want to update the content, you can unsubscribe that event handler and use data binding. Try this in your xaml:

    <local:MyCombox x:Name="mycombo" Height="60" Width="200" ItemsSource="{Binding Path=MyCollection}" SelectedItem="{Binding Path=MyItem}" SelectionChanged="mycombo_SelectionChanged">
    </local:MyCombox >
    

    and below in your cs file:

    public partial class MainWindow : Window
        {
            public List<string> MyCollection { get; set; }
            public string MyItem { get; set; }
            public MainWindow()
            {
                InitializeComponent();
                MyItem = "test";
                MyCollection = new List<string>();
                MyCollection.Add(MyItem);
                MyItem = "test1";
                MyCollection.Add(MyItem);
                this.DataContext = this;
    
                mycombo.SelectionChanged -= mycombo_SelectionChanged;
                mycombo.SelectedIndex = 1;
    
            }
    
            private void mycombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
            }
    

    I have created a sample project here. You will find SelectionChanged Event will not be triggered. Actually you even don't need to customize a ComboBox.

    However, If this event does not cause a performance issue, it is still recommended that you use it, as these events are designed like this in WPF.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.