示例#1
0
        /// <summary>
        /// INotifyCollectionChanged.CollectionChanged handler for updating DataGrid.SelectedItems when the source list changes
        /// </summary>
        private static void UpdateDataGridsOnSourceCollectionChanged(object source, NotifyCollectionChangedEventArgs collectionChangedArgs)
        {
            DataGridsAndInitiatedSelectionChange sourceListInfo = DataGridMultipleSelection.selectedItemsSources[source.GetHashCode()];

            // For each DataGrid that is bound to this list, is alive, and did not initate selection changes, update its selection
            sourceListInfo.InitiatedSelectionChange = true;
            IList sourceList = source as IList;

            Debug.Assert(sourceList != null, "SelectedItemsSource must be of type IList");
            DataGrid dataGrid = null;

            foreach (WeakReference dataGridReference in sourceListInfo.BoundDataGridReferences)
            {
                if (dataGridReference.IsAlive && !DataGridMultipleSelection.GetInitiatedSelectionChange(dataGridReference.Target as DataGrid))
                {
                    dataGrid = dataGridReference.Target as DataGrid;
                    UpdateDataGrid(dataGrid, sourceList, collectionChangedArgs);
                }
            }
            sourceListInfo.InitiatedSelectionChange = false;
        }
示例#2
0
        /// <summary>
        /// DataGrid.SelectionChanged handler to update the source list given the SelectionChangedEventArgs
        /// </summary>
        private static void UpdateSourceListOnDataGridSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedArgs)
        {
            DataGrid dataGrid            = sender as DataGrid;
            IList    selectedItemsSource = DataGridMultipleSelection.GetSelectedItemsSource(dataGrid) as IList;

            Debug.Assert(selectedItemsSource != null, "SelectedItemsSource must be of type IList");
            // If the source list initiated the changes then don't pass the DataGrid's changes back down to the source list
            if (!DataGridMultipleSelection.selectedItemsSources[selectedItemsSource.GetHashCode()].InitiatedSelectionChange)
            {
                DataGridMultipleSelection.SetInitiatedSelectionChange(dataGrid, true);
                foreach (object removedItem in selectionChangedArgs.RemovedItems)
                {
                    selectedItemsSource.Remove(removedItem);
                }

                foreach (object addedItem in selectionChangedArgs.AddedItems)
                {
                    selectedItemsSource.Add(addedItem);
                }
                DataGridMultipleSelection.SetInitiatedSelectionChange(dataGrid, false);
            }
        }