private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e) { bool wasDatePickerDataContextNull = false; DatePicker datePicker = sender as DatePicker; if (datePicker != null && datePicker.DataContext == null) { wasDatePickerDataContextNull = true; datePicker.DataContext = datePickerDataContext; } if (datePicker != null && datePicker.DataContext != null && columnForCellBeingEdited != null && columnForCellBeingEdited != "") { try { if (!e.AddedItems[0].Equals(datePicker.DataContext.GetType().GetProperty(columnForCellBeingEdited).GetValue(datePicker.DataContext, null))) { DataSourceCreator.SetProperty(e.AddedItems[0], datePicker.DataContext, datePicker.DataContext.GetType().GetProperty(columnForCellBeingEdited)); Graphic correspondingGraphic = DataSourceCreator.GetGraphicSibling(datePicker.DataContext); if (correspondingGraphic != null && correspondingGraphic.Attributes.ContainsKey(columnForCellBeingEdited)) { // Unsubscribing from graphic's AttributeValueChanged event to manually refresh corresponding row: correspondingGraphic.AttributeValueChanged -= Graphic_AttributeValueChanged; DateTime?selectedDate = e.AddedItems[0] as DateTime?; DateTime?dateToSet = null; if (selectedDate != null) { dateToSet = new DateTime(selectedDate.Value.Ticks, selectedDate.Value.Kind); } correspondingGraphic.Attributes[columnForCellBeingEdited] = dateToSet; if (wasDatePickerDataContextNull) { correspondingGraphic.RefreshRow(ItemsSource, GetGraphicIndexInGraphicsCollection(correspondingGraphic), objectType); datePickerDataContext = null; } // Subscribing back to the graphic's AttributeValueChanged event: correspondingGraphic.AttributeValueChanged += Graphic_AttributeValueChanged; } } } catch { } } }
public void RefreshRow(Graphic graphic) { #if SILVERLIGHT int idx = GetGraphicIndexInGraphicsCollection(graphic); IList gridRows = ItemsSource.AsList(); if (idx > -1 && idx < gridRows.Count) { try { // In Silverlight refreshing a row corresponding to a graphic causes to lose current selection. // Preserving index of currently selected items in the FeatureDataGrid: int selCount = SelectedItems.Count; int[] selIndexes = new int[selCount]; for (int i = 0; i < selCount; i++) selIndexes[i] = GetRowIndexInRowsCollection(SelectedItems[i]); // Unsubscribe from PagedCollectionView CollectionChanged event to perform a manual source // collection update: (ItemsSource as PagedCollectionView).CollectionChanged -= PagedCollectionView_CollectionChanged; graphic.RefreshRow((ItemsSource as ICollectionView).SourceCollection, idx, objectType); gridRows = ItemsSource.AsList(); // Refresh needed as a row in the ItemsSource has changed // Subscribing back to the PagedCollectionView CollectionChanged event handler: (ItemsSource as PagedCollectionView).CollectionChanged += PagedCollectionView_CollectionChanged; // Restoring the selection stored before updating the row (Silverlight only): SelectedItems.Clear(); for (int i = 0; i < selCount; i++) SelectedItems.Add(gridRows[selIndexes[i]]); } catch (Exception ex) { throw new ArgumentException(string.Format(Properties.Resources.FeatureDataGrid_RowUpdateFailed, idx.ToString()), ex); } } #endif }