重複した項目を含む ListBox があります。私が収集したものからListBox.SelectedItems
、重複の最初のインスタンスのみが返されますが、ユーザーが選択したすべてのアイテムに対してアクションを実行したい場合に問題が発生します。複数の重複を選択して を呼び出すとListBox.SelectedItems.Count
、常に が表示されます1
。一意であるかどうかに関係なく、すべてのアイテムのインデックスを取得する方法はありますか? (ListBox モードは複数に設定されています)。
同じアイテムが重複と見なされることを示す再現コードを追加しました。
Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<ListBox Height="288" HorizontalAlignment="Left" Margin="12,0,0,0" Name="listBox1" VerticalAlignment="Top" Width="276" SelectionMode="Multiple" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="313,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
コード:
ObservableCollection<string> fruits = new ObservableCollection<string>();
fruits.Add("Apple");
fruits.Add("Pear");
fruits.Add("Orange");
fruits.Add("Apple");
listBox1.ItemsSource = fruits ;
ボタンイベントをこれに接続しました:
MessageBox.Show(listBox1.SelectedItems.Count.ToString());
上を選択しApple
て ボタンをクリックすると、 に戻り1
ます。両方Apples
を選択すると、 が返され1
ます。と を選択するApple
とPear
、 が返され2
ます。