0
votes

I am trying to get the selected item from the ListBox using listbox_SelectionChanged() method, but it does not seem to work. Could you tell me what is the best way to get the selected item out of listbox. the code I tried is bellow.

your help much appreciated.

XAML

<ListBox
            x:Name="lbSkills"
            Grid.Row="1"
            Margin="10,0,10,10" SelectionChanged="LbSkills_SelectionChanged">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="0,0,0,1" BorderBrush="Beige">
                            <Grid Width="auto" HorizontalAlignment="Stretch">
                                <TextBlock VerticalAlignment="Center" FontSize="26" Grid.Column="0" Foreground="Black" Text="{Binding SkillDescription}"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

XAML.cs - I have also tried commented code, but unable to get the selected item

 private async void LbSkills_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //var addedItems = e.AddedItems;
        //string selectedSkillString = "None";
        //if (addedItems.Count > 0)
        //{
        //   var selectedSkill = addedItems[0];
        //    selectedSkillString = selectedSkill.ToString();
        //}

        //lbSkills.SelectedItem.ToString();

        MessageDialog msgBox = new MessageDialog(e.AddedItems.ToString());
        await msgBox.ShowAsync();
    }
1
I tried your code and the evend is fired properly when a item in the list is selected, it's both in the lbSkills.SelectedItem and in e.AddedItems[0]alek kowalczyk
lbSkills.SelectedItem properly shows the selected item. try removing async / await and check.AnjumSKhan
Hi ANjum, i tried removing the async/await but still the same. I am only getting the path but not the item selected.Just to mentions I am trying this in Windows 8.1 (Not the phone).BRDroid

1 Answers

0
votes
  1. First of all check what is the DataConntext or ItemsSource of you ListBox (it have to be an ObservableCollection to avoid the memory leaks).
  2. Check if there is a Binding errors in the Output window.
  3. Check if there is a correcct property to bind to.
  4. Try the solution the next solution: As I can understand you, the problem is that the added items of event argument doesn't contains the current selected item. But there is no any problem with your code. It returns the actual model (Skill) when I used it. But if you apply ToString() metod on it, you won't get the real model, the result will be just the full name of a class (<Full.Assembly.Path>.<Class_Name>). If you want to get the model instance you have to cast or safely cast the e.AddedItems content or you have to override the ToString() method in your model class. From another hand if you want to get the ListBoxItem itself for some reason try to use the next code:

     var listBox = sender as ListBox;
        var selected = e.AddedItems.Cast<object>().FirstOrDefault();
        var container = listBox.ItemContainerGenerator.ContainerFromItem(selected);
    

regards