Displaying selected item of a wpf ComboBox in a messagebox

I have the following ComboBox in XAML Code

<ComboBox Name="angleComboBox" SelectionChanged="angleComboBox_SelectionChanged">
                    <ComboBoxItem>Degree</ComboBoxItem>
                    <ComboBoxItem>Radian</ComboBoxItem>
</ComboBox>

along with its following event handler below

private void angleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selection = angleComboBox.SelectionBoxItem as string;
            MessageBox.Show(selection);
        }

What I am trying to do is get the messagebox to display whatever I am selecting in the combobox for example a messagebox display the word Degree when Degree is selected from the ComboBox. For some reason selecting Degree in the ComboBox yields and empty message box and selecting Radian yields a messagebox with the word Degree instead of Radians.

a_anis3000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikebirtCommented:
Hi,

try using the selected item instead. it's a comboBoxItem so you're after the content property of that. I've implemented the change to the handler for you below:

HTH

Mike
        private void angleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem cbi = angleComboBox.SelectedItem as ComboBoxItem;
            string selection = cbi.Content.ToString();
            MessageBox.Show(selection);
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_Katka_Commented:
Hi, you've to process e.AddedItems and e.RemovedItems correctly. SelectionBoxItem will only show the current item, but it's set to the current value at the time the event is fired.

regards,
Kate
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.