Xceed DataGrid for WPF v7.3 Documentation
Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.3 / Xceed DataGrid for WPF / Code Snippets / Receiving Notifications when the Selected Item(s) Changes

In This Topic
    Receiving Notifications when the Selected Item(s) Changes
    In This Topic

    The following example demonstrates how to provide value-changed handlers for the SelectedItem and SelectedIndex dependency properties in order to be notified when their value changes.

    VB.NET
    Copy Code
    Dim selectedItemDescriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.SelectedItemProperty, GetType( DataGridControl ) )
    selectedItemDescriptor.AddValueChanged( Me.EmployeesGrid, Me.SelectedItemChanged )
    C#
    Copy Code
    DependencyPropertyDescriptor selectedItemDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.SelectedItemProperty, typeof( DataGridControl ) );
    selectedItemDescriptor.AddValueChanged( this.EmployeesGrid, this.SelectedItemChanged );

    The SelectedItems property is also a dependency property; however, using a value-changed handler will not function in this case since the values of the collection are modified and not the collection itself. Although the SelectedItems property is exposed as an IList it is actually an ObservableCollection; therefore, in order to be notified when its content (i.e., the items that are selected) changes, it can be cast as an INotifyCollectionChanged and subscribe to its CollectionChanged event.

    VB.NET
    Copy Code
    AddHandler CType( Me.EmployeesGrid.SelectedItems, INotifyCollectionChanged ).CollectionChanged, AddressOf Me.Window1_CollectionChanged
    C#
    Copy Code
    ( ( INotifyCollectionChanged )this.EmployeesGrid.SelectedItems ).CollectionChanged += new NotifyCollectionChangedEventHandler( Window1_CollectionChanged );