0

I can't get string value from Listbox after selected one item.

In my Listbox I have the Image (blinding source) and TextBlock (blinding source).

My code at .xaml page:

        <ListBox Name="carListBox" Height="431" Canvas.Left="28" Canvas.Top="65" Width="446" SelectionChanged="ListBoxOnSelection">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Name="brandImage" Source="{Binding Image}" Width="100" Height="150"></Image>
                        <Image Name="carImage" Source="{Binding Image}" Width="150" Height="150"></Image>
                        <TextBlock Name="textDisplay" Text="{Binding ShowDetail}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My code at .xaml.cs page (C#)

private void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)

    {
       MessageBox.Show(carListBox.SelectedItem.ToString());
       string saveData = carListBox.SelectedItem.ToString();

    }

MessageBox can't show string value and I can't get value after user selected.

MessageBox show [1]https://i.sstatic.net/5l79W.jpg

1 Answer 1

0

You have to somehow get the Car object before you can have access to it's properties.

Something like this:

private void ListBoxOnSelection(object sender, SelectionChangedEventArgs args)
{
 Car myCar=carListBox.SelectedItem as Car;
 if(myCar != null)
   MessageBox.Show(myCar.ShowDetail); //or any property.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped. You can accept the answer to show that it's helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.