Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are multiple options:</p> <ol> <li>Use selected item's index <code>listBox.SelectedIndex</code> to get the index of the selected property which will correspond to the index in your source (it might not if you filter the collection using collection source, but I think that is not the case)</li> <li>Use selected item <code>listBox.SelectedItem</code> this will return the SelectedItem which will contain your object. (Note, that if your selection mode set to multiple, this will return only the firstly selected item)</li> <li>Use <code>SelectemItems</code>. It will allow you to get an array of selected<br> items (Note: this should be normally used only when your list's<br> selection mode is set to multiple)</li> <li>Use <code>SelectedValue</code>, which will contain the value of the SelectedItem (this will save you and extra step.</li> <li>Use arguments of the Selection changed event AddedItems.</li> </ol> <p>Bellow is the code snippet of 3 options above. x, y, z will all be your selected names (e.g. "Mike")</p> <p>XAML:</p> <pre><code>&lt;ListBox x:Name="lb" ItemsSource="{Binding Names}" SelectionChanged="NameChanged" /&gt; </code></pre> <p>Code behind:</p> <pre><code> public class Person { public string Name { get; set; } public override string ToString() { return Name; } } private List&lt;Person&gt; people = new List&lt;Person&gt; { new Person{Name = "Lewis"}, new Person{Name = "Peter"}, new Person{Name = "Brian"} }; public List&lt;Person&gt; People { get { return this.people; } set { this.people = value; } } private void NameChanged(object sender, SelectionChangedEventArgs e) { var x = this.people[lb.SelectedIndex]; var y = lb.SelectedItem; var z = lb.SelectedItems[0]; var h = lb.SelectedValue; var u = e.AddedItems[0]; var person = e.AddedItems[0] as Person; if (person != null) { var result = person.Name; } } </code></pre> <p>For the differences between SelectedValue and SelectedItem refer here <a href="https://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath">SelectedItem vs SelectedValue</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload