Note that there are some explanatory texts on larger screens.

plurals
  1. POGet all selected items in ListBox (including duplicates)
    primarykey
    data
    text
    <p>I have a ListBox that contains duplicate items. From what I gather, <code>ListBox.SelectedItems</code> will only return the first instance of a duplicate, but this causes problems when I want to perform an action on all items a user has selected. When I select multiple duplicates and call <code>ListBox.SelectedItems.Count</code>, I always get <code>1</code>. Is there a way to get the indexes of all items, regardless if they're unique or not? (ListBox mode is set to Multiple).</p> <p>Added repro code demonstrating that the same item is considered a duplicate.</p> <p>Xaml:</p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;ListBox Height="288" HorizontalAlignment="Left" Margin="12,0,0,0" Name="listBox1" VerticalAlignment="Top" Width="276" SelectionMode="Multiple" /&gt; &lt;Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="313,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /&gt; &lt;/Grid&gt; </code></pre> <p>code:</p> <pre><code>ObservableCollection&lt;string&gt; fruits = new ObservableCollection&lt;string&gt;(); fruits.Add("Apple"); fruits.Add("Pear"); fruits.Add("Orange"); fruits.Add("Apple"); listBox1.ItemsSource = fruits ; </code></pre> <p>I just wired up a button event to this:</p> <pre><code>MessageBox.Show(listBox1.SelectedItems.Count.ToString()); </code></pre> <p>Select the top <code>Apple</code> and click the button, it will return <code>1</code>. Select both <code>Apples</code> and it will return <code>1</code>. Select an <code>Apple</code> and <code>Pear</code>, it will return <code>2</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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