Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to realize a general and simple function: in a combox, get the selected item text through user selection in a WPF ComboBox, but I could not find a way to do it even after search internet. Unbelievable. Hope anyone can help me.

the XAML and c# code are as follows. I have given the comments and my question as well the code. Note that I have tried the similar functions in both MainWindow() and Selectionchanged event. The problem occurs only in the latter function. But I need get the selected item, e.g. "item1" when I select the combox item to be the first item. But it gives an error (See below) when starting the debugging.

strangly, the XAML code can not be fully copied to here. however, you should assume the xaml code has no any problem. it is not complete only due to the editor here.

C#
<ComboBox x:Name="ComBoxTest" Height="50" SelectionChanged="ComBoxTest_SelectionChanged" SelectedValuePath="Content">

        <ComboBoxItem IsSelected="True">item1</ComboBoxItem>

        <ComboBoxItem>item2</ComboBoxItem>

</ComboBox>



C#
namespace WPFComboBoxTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        
 MessageBox.Show(ComBoxTest.SelectedItem.ToString());//System.Windows.Controls.ComboxItem:item1
 MessageBox.Show(ComBoxTest.SelectedValue.ToString());//item1 (OK)
string text = ((ComboBoxItem)ComBoxTest.SelectedItem).Content.ToString();//item1 (Ok, too)
            MessageBox.Show(text);
           
        }

        private void ComBoxTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show(ComBoxTest.SelectedItem.ToString());//System.Windows.Controls.ComboxItem, only, after starting the programm, no item1, which is selected by default i XAML code. But after starting programm, it shows System.Windows.Controls.ComboxItem:item1 when I select the first item
            MessageBox.Show(ComBoxTest.SelectedValue.ToString());//problem, when start debugging:  an unhandled exception of teyp 'System.Refection.TragetInvocationException' occured. I think this will work if I really hit the ComboBox and select one item. But I have no chance since this error occurs once starting the programm.
            string text = ((ComboBoxItem)ComBoxTest.SelectedItem).Content.ToString();//same problem
            MessageBox.Show(text);
            //my question: how to deal with this problem?
        }
    }
}


What I have tried:

tried different ways, selecteditem, selectedvalue, items, none works.
search also internet, but it is not successful.
Posted
Updated 27-Jun-16 0:04am
v10

1 solution

Remove this from XAML :
SelectedValuePath="Content"

and exception is gone...
 
Share this answer
 
Comments
Seraph_summer 27-Jun-16 6:12am    
thanks for your comment. Unfortunately, what you said seems not right. The problem is independent on SelectedValuePath="Content" exists or not.
Seraph_summer 27-Jun-16 6:13am    
you can easily try it in WPF by spending some minutes.
[no name] 27-Jun-16 6:34am    
I tried it before I answer your question. If SelectedValuePath="Content" is there, then ComboBoxTest.SelectedValue is null. If you remove it, then ComboBoxTest.SelectedValue is not null. I'm using VS2015 Update 2 with .NET 4.0 and Windows 10.
Seraph_summer 27-Jun-16 7:05am    
really, then it is more strange here. I have tried with both VS2015 community version on Win10 and VS2013 express version on Win7. It shows the same problem.
Have you tried exactly the same code as mine?
Seraph_summer 27-Jun-16 7:15am    
I found one stupid solution as follows

string[] textsplit = ComBoxTest.SelectedItem.ToString().Split(':');
if (textsplit.Length>1)
MessageBox.Show(textsplit[1].ToString());

but, I really want to understand the root cause or mechanism here. Can it be a bug? I do not think so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900