MVVM ライトを使用しており、複数選択できるリスト ボックスがあります。私のMainpage.xamlには
<ListBox Name="ListBox1" ItemsSource="{Binding Items}" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" Margin="15,15,18,0" SelectionMode="Multiple" Height="100" />
MainPage.xaml.cs にはあります (何らかの理由で依存関係プロパティを使用したくありません)。
MainPage()
{
ListBox1.SelectionChanged = new SelectionChangedEventHandler(ListBox1_SelectionChanged);
}
void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
var viewModel = listBox.DataContext as MainViewModel;
viewModel.SelectedItems.Clear();
foreach (string item in listBox.SelectedItems)
viewModel.SelectedItems.Add(item);
}
これは正常に機能し、MainViewModel にバインドします。しかし、ページが読み込まれると、コレクション項目の最初の項目がデフォルトで選択されます。これを実装する方法を教えてください