1

Have spent a good while Googling and searching stackoverflow but either the solutions aren't quite what I'm looking for, or I'm not implementing quite right. Don't think it's a big deal so hopefully someone can provide a solution (bit of a SO noob so any more details needed let me know).

I'm trying to get the values of multiple items from a listbox in a W8.1 universal XAML app so I can pass them to a SQLite DB (am thinking as a string using a comma delimited 'join' statement). The data source for the ListItems is currently manually set for various values.

In the meantime I'm setting a label to the text of the string as a test and wasn't getting any joy from using Checkboxes within the listbox, so I've changed to ListBoxItems, however I'm not getting the content through, just the following (when selecting two list items):

Windows.UI.Xaml.Controls.ListBoxItem, Windows.UI.Xaml.Controls.ListBoxItem

Obviously I'm after the Content, which should be 'Developer' and 'Tester'. Here's the XAML for the ListBox and my current code:

<TextBlock x:Name="lblAreas" HorizontalAlignment="Left" Margin="548,200,0,0" TextWrapping="Wrap" Text="Areas of Interest" VerticalAlignment="Top" RenderTransformOrigin="1.143,1.692" FontSize="13.333" Height="32" Width="336"/>
    <ListBox x:Name="lbAreas" HorizontalAlignment="Left" Height="231" Margin="548,241,0,0" VerticalAlignment="Top" Width="194" SelectionMode="Multiple">
        <ListBoxItem Content="Developer"/>
        <ListBoxItem Content="Tester"/>
    </ListBox>

Code behind:

var listSelectedItems = lbAreas.SelectedItems.ToList();
string strSelectAreas = string.Join(", ", listSelectedItems);
lblAreas.Text = strSelectAreas;

Hopefully that all makes sense, I've tried various methods from SO and elsewhere that look like they should work but not quite there! Thanks for any help.

newton
  • 23
  • 5

1 Answers1

0

SelectedItems returns a list of objects, and the string representation of those objects is the default (which is the name of the class). You actually the Content property of each selected item, so something like:

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Select(i => i.Content));

lblAreas.Text = selectAreas;

Note: you might have to cast each item to a ListBoxItem:

string selectAreas = string.Join(", ", lbAreas.SelectedItems.Cast<ListBoxItem>()
                                                            .Select(i => i.Content));
Dave Zych
  • 21,581
  • 7
  • 51
  • 66