Articles in this section
Category / Section

How to bind the SelectedItems property to ViewModel property in WPF DataGrid?

3 mins read

 

WPF DataGrid .SelectedItems is a collection property. So, whenever the SelectedItems collection is changed, the property bound with SelectedItems does not get notified for this change. However, you can achieve this by creating an attached property for SelectedItems.

The following is an attached property created for SfDataGrid and the property value is updated based on SfDataGrid.SelectedItems.CollectionChanged event.

C#

class SfDataGridHelper
    {
        public static void SetSelectedItems(DependencyObject element, object value)
        {
            if (element is SfDataGrid)
                element.SetValue(SelectedItemsProperty, value);
            else
                throw new NotSupportedException("This property can be used only with SfDataGrid");
        }
        public static object GetSelectedItems(DependencyObject element)
        {
            return (object)element.GetValue(SelectedItemsProperty);
        }
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
        "SelectedItems", typeof(ObservableCollection<Object>), typeof(SfDataGridHelper), new PropertyMetadata(new ObservableCollection<Object>(), OnSelectedItemsChanged));
        private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            var sfDataGrid = d as SfDataGrid;
            if (sfDataGrid == null)
                return;
            //SfDataGridHelper.SelectedItems property updated based on SfDataGrid.SelectedItems Collectionchanged event.
            sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>
            {
                SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);
            };
        }
    }

SfDataGrid’s SelectedItems values are set to the attached property by using SelectedItems.Collectionchanged event.

The following is the ViewModel property bound to the attached property that you created in the above code example.

XAML

<syncfusion:SfDataGrid x:Name="dataGrid"
                               Grid.Row="0"
                               AutoGenerateColumns="False"
                               AllowEditing="True"
                               local:SelectedItemsBehaviour.SelectedItems=
                               "{Binding SelectedItems, Mode=TwoWay,
                               UpdateSourceTrigger=PropertyChanged}"
                               AllowDeleting="True"
                               AllowResizingColumns="True"
                               AllowResizingHiddenColumns="True"
                               HideEmptyGridViewDefinition="True"
                               ItemsSource="{Binding GDCSource}"
                               NavigationMode="Cell"
                               SelectionMode="Extended"
                               ShowGroupDropArea="True"
                               ShowRowHeader="True">

Now, whenever the SelectedItems collection is changed the ViewModel property gets notified via the attached property.

You can download the example from GitHub

Conclusion

I hope you enjoyed learning about how to bind the SelectedItems property to ViewModel property in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied