title: Wpf MutliSelect DataGrid with custom datagrid
body: <p>I'm aware that there are several posts about how to implement datagrid mutliselection.</p>
  <p>I've found the open issue on Github (<a href="https://github.com/dotnet/wpf/issues/3140" rel="nofollow noreferrer">https://github.com/dotnet/wpf/issues/3140</a>).
  The user ice1e0 mentioned a solution with a <code>CustomDataGrid</code> class.</p>
  <pre class="lang-cs prettyprint-override"><code>public class CustomDataGrid : DataGrid
  {
          public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(default(IList), OnSelectedItemsPropertyChanged));
  
          protected override void OnSelectionChanged(SelectionChangedEventArgs e)
          {
              base.OnSelectionChanged(e);
              SetValue(SelectedItemsProperty, base.SelectedItems);
          }
  
          public new IList SelectedItems
          {
              get =&gt; (IList)GetValue(SelectedItemsProperty);
              set =&gt; throw new Exception(&quot;This property is read-only. To bind to it you must use 'Mode=OneWayToSource'.&quot;);
          }
  
          private static void OnSelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              ((CustomDataGrid)d).OnSelectedItemsChanged((IList)e.OldValue, (IList)e.NewValue);
          }
  
          protected virtual void OnSelectedItemsChanged(IList oldSelectedItems, IList newSelectedItems)
          {
          }
  }
  </code></pre>
  <p>So, I tried to implement it, so I copied the class.
  And my xaml looks something like that:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;Project1:CustomDataGrid ItemsSource=&quot;{Binding Data}&quot; SelectionMode=&quot;Extended&quot; SelectionUnit=&quot;FullRow&quot; IsReadOnly=&quot;True&quot; SelectedItems=&quot;{Binding SelectedComputers, Mode=OneWayToSource}&quot; /&gt;
  </code></pre>
  <p>and my ViewModel:</p>
  <pre class="lang-cs prettyprint-override"><code>[ObservableProperty]
  ObservableCollection&lt;string&gt; _selectedComputers;
  </code></pre>
  <p>I use CommunityToolkit.Mvvm (v8.0).</p>
  <p>It compiles but <code>SelectedComputers</code> is null, after I select one or multiple items.</p>
  <p><strong>UPDATE:</strong></p>
  <p>The issue seems that</p>
  <pre class="lang-cs prettyprint-override"><code>        void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              this.SelectedItemsList = this.SelectedItems;
          }
  </code></pre>
  <p>doesn't work. <code>SelectedItemsList</code> is always null. The problem is mentioned in the other thread: <a href="https://stackoverflow.com/questions/22868445/select-multiple-items-from-a-datagrid-in-an-mvvm-wpf-project">Select multiple items from a DataGrid in an MVVM WPF project</a></p>
  <p>The suggested solution by M463:</p>
  <blockquote>
  <p>this.SelectedItemsList = this.SelectedItems; did not work for me, as SelectedItemsList was always set to null. However, changing the code to foreach (var item in this.SelectedItems) { this.SelectedItemsList.Add(item); } did the trick. Please note, that this requires you to call this.SelectedItemsList.Clear(); in advance, so the items of SelectedItemsList will not get duplicated.</p>
  </blockquote>
  <p>When I try the solution, after selected a row the application crashes with the error:</p>
  <pre><code>System.ArgumentException: 'The value &quot;System.Data.DataRowView&quot; is not of type &quot;System.String&quot; and cannot be used in this generic collection.
  Parameter name: value'
  </code></pre>