简体   繁体   中英

Getting the text value selected in at ListBox in C#

I have a list box

  <Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
    <ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">
        <ListBox Height="100" Width="100" SelectionChanged="ListBox_SelectionChanged_1">
            <ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
            <ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
            <ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
            <ListBoxItem x:Name="ListBoxFact" Content="FACT"/>
        </ListBox>
    </ListBox>

This was created by dragging the listbox icon from the tool bar. I added items and their values.
Now I am trying to just get the text value of the selected item.

 private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        string text = (string)ListBox1.SelectedValue;
        MessageBox.Show(text);

I have also tried SelectedItem

string text = (string)ListBox1.SelectedItem;

But the message box is always blank.
This should be simple, but I have been working on it for hours, and trying every suggestion or answer on stackoverflow. Most suggestions do not even compile. For example:

  string selected = listBox1.GetItemText(listBox1.SelectedValue);

Will not compile. GetItemText is not found. I am using Visual Studio 17. "'ListBox does not contain a definition for 'GetItemText'..."

Any thoughts? Please advise. Thanks.

Thanks for the comment, Charles. I did that. Playing further, now I get

 System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.ListBoxItem' to type 'System.String'.' string text = (string)ListBox1.SelectedItem; 

In the markup, SelectedIndex is set to -1 which means there is no selection. In this case, SelectedValue and SelectedItem both return null. You can solve this either by setting SelectedIndex to a value between 0 and 3 or by preparing your code to cope with a null value in SelectedValue and SelectedItem , eg

string text = (ListBox1.SelectedItem as ListBoxItem)?.Content?.ToString();

This won't raise an error so that the user can select an item afterwards. With a selection, the text should be displayed as expected.

As indicated by Charles May, your XAML shows that your ListBox is within another ListBox, which is why you're getting errors being raised..

The event being called "ListBox_SelectionChanged_1" is bound to the ListBox object inside ListBox1, which is unnamed.

I believe that the behaviour you are looking for would be fixed like this:

XAML:

<Label Content="Report" HorizontalAlignment="Left" Height="47" Margin="36,75,0,0" VerticalAlignment="Top" Width="63"/>
    <ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="121" Margin="84,75,0,0" VerticalAlignment="Top" Width="102" SelectionChanged="ListBox_SelectionChanged" SelectedIndex="-1">            
        <ListBoxItem x:Name="ListBoxFAT" Content="FAT"/>
        <ListBoxItem x:Name="ListBoxNUMI" Content="NUMI"/>
        <ListBoxItem x:Name="ListBoxSSID" Content="SSID"/>
        <ListBoxItem x:Name="ListBoxFact" Content="FACT"/>            
    </ListBox>

Code Behind:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   string text = ((sender as ListBox)?.SelectedItem as ListBoxItem)?.Content.ToString();
   MessageBox.Show(text);
}

Or at least something close to this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM