【UWP】ContentDialog内に配置したListViewの初期複数選択を指定するには


## TL;DL

ContentDialog.Opened時に複数選択する値を設定する

原因の推測：ダイアログ表示前の段階ではListView内部の選択元となるアイテムがまだ作成されていない？

## コード例

```xml:SelectItemDialog.xaml
<ContentDialog
  x:Name="MyDialog"
  x:Class="MyApplication.Presentation.Views.Dialogs.SelectItemDialog"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:MyApplication.Presentation.Views.Dialogs"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  >
  <Grid>
    <ListView 
      x:Name="MyListView"
      ItemsSource="{x:Bind ItemsSource, Mode=OneWay}"
      DisplayMemberPath="{x:Bind DisplayMemberPath, Mode=OneWay}"
      SelectionMode="Multiple" 
      IsMultiSelectCheckBoxEnabled="True"
      SingleSelectionFollowsFocus="False"
      >
    </ListView>
  </Grid>
</ContentDialog>
```


```cs:SelectItemDialog.xaml.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

// コンテンツ ダイアログの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=234238 を参照してください

namespace TsubameViewer.Presentation.Views.Dialogs
{
    public sealed partial class SelectItemDialog : ContentDialog
    {
        public SelectItemDialog(string title, string confirmButtonText)
        {
            this.InitializeComponent();
            Title = title;
            PrimaryButtonText = confirmButtonText;
            CloseButtonClick += SelectItemDialog_CloseButtonClick;
            PrimaryButtonClick += SelectItemDialog_PrimaryButtonClick;

            Opened += SelectItemDialog_Opened;
        }

        private void SelectItemDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
        {
            if (_selectItems != null)
            {
                MyListView.SelectedItems.Clear();

                int index = 0;
                foreach (var item in ItemsSource)
                {
                    if (_selectItems.Contains(item))
                    {
                        MyListView.SelectRange(new ItemIndexRange(index, 1));
                    }

                    index++;
                }

                _selectItems = null;
            }
        }

        IEnumerable<object> _selectItems;

        public void SetSelectedItems(IEnumerable<object> selection)
        {
            _selectItems = selection;
        }

        public IList<object> GetSelectedItems()
        {
            return MyListView.SelectedItems.ToList();
        }

        public IList ItemsSource
        {
            get { return (IList)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IList), typeof(SelectItemDialog), new PropertyMetadata(null));

        public string DisplayMemberPath
        {
            get { return (string)GetValue(DisplayMemberPathProperty); }
            set { SetValue(DisplayMemberPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DisplayMemberPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DisplayMemberPathProperty =
            DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(SelectItemDialog), new PropertyMetadata(null));


    }
}
```