Getting selected item string from bound ListBox

18,145

Solution 1

You're binding to the ItemString in the DataTemplate's TextBlock and the Item Collection in the ListView. As such the SelectedValue will be of the Item type. You should actually be doing something like this in your Tap handler to get at the ItemString's value...

private void listBoxtrend_Tap(object sender, GestureEventArgs e)
{
    selectedText = "";

    var selected = listBox.SelectedValue as Item;
    selectedText = selected.ItemString;

    MessageBox.Show(selectedText);
}

In your example, the ToString is printing the name of the class. You could also override ToString in your Item model to be whatever you want the string to be.

Note: the types and such may be a bit off, I guessed a bit based off of what you wrote in your question. Also, there is no need to set selectedText to an empty string that will just be overwritten in the third line above. I wanted to keep it so you could get some idea of what I changed in your code.

Solution 2

It's very simple, try following:

string selectedText = ListBox.GetItemText(ListBox.SelectedItem);

Solution 3

You need to also set the SelectedItem of the Listbox to something.

SelectedItem = {Binding SelectedItem}

and rename your ItemsSource to "Items" as that makes more sense.

Your SelectedItem in your codebehind or your ViewModel should then contain a property:

public class Item
{
    public string ItemString { get;set; }
}

Solution 4

Try This...

string ListBoxConent  = ((ListBoxItem)listbox.SelectedItem).Content.ToString();
Share:
18,145
M_K
Author by

M_K

Updated on June 04, 2022

Comments

  • M_K
    M_K almost 2 years

    I'm having a problem with getting a string from a bound textblock within a listbox, when I use the code below, I can bind the listbox and the listbox has items showing up, but when the item in the list is clicked I don't get the proper string, I print a message box a message with objects names like

    "MyApp.Item"

    shows up instead. myApp is the name of the app and Item is the name of my model that I am binding to the listbox. The proper text from the selected item showed up when the listbox was not binded.

    private void listBoxtrend_Tap(object sender, GestureEventArgs e)
    {
        selectedText = "";
    
        selectedText = listBox.SelectedValue.ToString();
    
        MessageBox.Show(selectedText);
    }
    

    xml

    <ListBox ItemsSource="{Binding Item}" Foreground="RoyalBlue" 
        Height="395" HorizontalAlignment="Center" 
        Margin="12,111,0,0" Name="listBox" 
        VerticalAlignment="Top" Width="438"
        TabIndex="10"  Tap="listBox_Tap" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" FontSize="26" HorizontalAlignment="Left"
                    Name="tblItem" Text="{Binding ItemString}"
                    VerticalAlignment="Top" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    I'd really appreciate if you could help me thanks