private void GraphicsLayer_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Graphics") { GraphicsLayer graphicsLayer = sender as GraphicsLayer; // Resetting internal graphic collection and respective event handlers: UnregisterGraphicCollectionEventHandlers(); currentGraphicCollection = (sender as GraphicsLayer).Graphics; RegisterGraphicCollectionEventHandlers(); SetItemsSource((GraphicsLayer != null && GraphicsLayer.Graphics != null) ? (IList <Graphic>)GraphicsLayer.Graphics : new List <Graphic>()); ResetLayout(); // Restoring previously selected graphics (if any): if (GraphicsLayer != null) { RestorePreviousSelection(GraphicsLayer.SelectedGraphics); } } else if (e.PropertyName == "SelectionCount") { if (!isSelectionChangedFromFeatureDataGrid && SelectedItems != null && SelectedGraphics != null) { if (SelectedGraphics.Count == 0) { SelectedItems.Clear(); } var selectedItemsCount = SelectedItems.Count; for (int i = selectedItemsCount - 1; i >= 0; i--) { var g = DataSourceCreator.GetGraphicSibling(SelectedItems[i]); if (!g.Selected) { SelectedItems.Remove(SelectedItems[i]); } } foreach (var g in SelectedGraphics) { var r = GetCorrespondingGridRow(g); if (r != null && !SelectedItems.Contains(r)) { SelectedItems.Add(r); } } } ShowNumberOfRecords(); } else if (e.PropertyName == "HasEdits") { SetSubmitButtonEnableState(); } }
/// <summary> /// Gets the corresponding row in <see cref="FeatureDataGrid"/> for the graphic. /// </summary> /// <param name="graphic">The graphic.</param> /// <returns></returns> private object GetCorrespondingGridRow(Graphic graphic) { if (graphic != null && ItemsSource != null) { foreach (var item in ItemsSource) { if (DataSourceCreator.GetGraphicSibling(item) == graphic) { return(item); } } } return(null); }
/// <summary> /// Raises the <see cref="E:System.Windows.Controls.DataGrid.RowEditEnded"/> event. /// </summary> /// <param name="e">The event data.</param> protected override void OnRowEditEnded(DataGridRowEditEndedEventArgs e) { base.OnRowEditEnded(e); if (e.EditAction == DataGridEditAction.Commit) { Graphic relatedGraphic = DataSourceCreator.GetGraphicSibling(e.Row.DataContext); if (relatedGraphic != null) { try { e.Row.RefreshGraphic(relatedGraphic, objectType); } catch { } } } }
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 { } } }
private void GoToVisualState(Graphic graphic, string stateName) { if (rowsPresenter != null && rowsPresenter.Children != null) { foreach (UIElement element in rowsPresenter.Children) { DataGridRow row = element as DataGridRow; if (row != null) { Graphic g = DataSourceCreator.GetGraphicSibling(row.DataContext); if (graphic.Equals(g)) { VisualStateManager.GoToState(row, stateName, true); break; } } } } }
/// <summary> /// Raises the <see cref="E:System.Windows.Controls.DataGrid.CellEditEnded"/> event. /// </summary> /// <param name="e">The event data.</param> protected override void OnCellEditEnded(DataGridCellEditEndedEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { Field field = e.Column.GetValue(FieldColumnProperty) as Field; if (field != null && featureLayer != null && featureLayer.LayerInfo != null) { if (field.Domain is CodedValueDomain || featureLayer.LayerInfo.TypeIdField == field.Name || FieldDomainUtils.IsDynamicDomain(field, featureLayer.LayerInfo)) { // When cell edit ends update the graphic with the cell change. var xObject = e.Row.DataContext; var graphic = DataSourceCreator.GetGraphicSibling(xObject); xObject.RefreshGraphic(graphic, xObject.GetType()); } } } base.OnCellEditEnded(e); }
/// <summary> /// Selects/deselects related graphic objects in the GraphicsLayer /// when related grid rows have been selected/deselected by the user. /// </summary> /// <param name="rowsToLookup"></param> /// <param name="shouldSelectGraphics"></param> private void SelectGraphics(IList rowsToLookup, bool shouldSelectGraphics) { foreach (object objRow in rowsToLookup) { Graphic graphic = DataSourceCreator.GetGraphicSibling(objRow); if (graphic != null && GraphicsLayer != null && GraphicsLayer.Contains(graphic)) { graphic.Selected = shouldSelectGraphics; if (shouldSelectGraphics) { currentRecordNumber = GetRowIndexInRowsCollection(objRow); } else { if (SelectedGraphics.Count == 0) { currentRecordNumber = -1; } } } } }
/// <summary> /// Updates contents of the ItemsSource when FeatureDataGrid's associated graphic collection changes. /// </summary> /// <param name="sender">Observable collection of Graphic.</param> /// <param name="e">Collection changed event arguments.</param> private void UpdateItemsSource(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { IList newItems = e.NewItems; IList oldItems = e.OldItems; if (ItemsSource == null) { SetItemsSource(sender as ObservableCollection <Graphic>); } else { if ((ItemsSource as PagedCollectionView) != null) { object currentItem = (ItemsSource as PagedCollectionView).CurrentItem; if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { var features = (from object a in ItemsSource select DataSourceCreator.GetGraphicSibling(a)); newItems = (sender as ObservableCollection <Graphic>).Except(features).ToList(); oldItems = features.Except(sender as ObservableCollection <Graphic>).ToList(); } if (newItems != null && newItems.Count > 0) // New item(s) added { bool shouldResetItemsSource = false; IEnumerator enumItemsSource = ItemsSource.GetEnumerator(); if (enumItemsSource != null) { if (enumItemsSource.MoveNext()) { if (!AllAttributesMatch(enumItemsSource.Current.GetType().GetProperties(), (newItems[0] as Graphic).Attributes)) { shouldResetItemsSource = true; } } else { shouldResetItemsSource = true; } } if (shouldResetItemsSource) { UnregisterGraphicCollectionEventHandlers(); SetItemsSource(sender as ObservableCollection <Graphic>); IEnumerator enumAddedGraphics = newItems.GetEnumerator(); while (enumAddedGraphics.MoveNext()) { Graphic graphic = enumAddedGraphics.Current as Graphic; if (graphic != null) { graphic.AttributeValueChanged += Graphic_AttributeValueChanged; } } } else { IEnumerator enumAddedGraphics = newItems.GetEnumerator(); List <Graphic> selected = GraphicsLayer.SelectedGraphics.ToList(); while (enumAddedGraphics.MoveNext()) { Graphic graphic = enumAddedGraphics.Current as Graphic; if (graphic != null) { if (graphic.Selected) { selected.Add(graphic); } ItemsSource.AddToDataSource(featureLayerInfo, graphic, objectType); graphic.AttributeValueChanged += Graphic_AttributeValueChanged; } } RestorePreviousSelection(selected); } } if (oldItems != null && oldItems.Count > 0) // Item(s) removed { int selCount = SelectedItems.Count; // In Silverlight removing a graphic from the GraphicsCollection causes to lose current // selection in both GraphicsLayer and the FeatureDataGrid. // Preserving selected items in the FeatureDataGrid: List <Graphic> selItems = new List <Graphic>(selCount); for (int i = 0; i < selCount; i++) { var row = SelectedItems[i]; var graphic = DataSourceCreator.GetGraphicSibling(row); selItems.Add(graphic); } IEnumerator enumRemovedGraphics = oldItems.GetEnumerator(); while (enumRemovedGraphics.MoveNext()) { Graphic graphic = enumRemovedGraphics.Current as Graphic; int idxInItemsSource = GetRowIndexInItemsSource(graphic); if (graphic != null && idxInItemsSource > -1) { if (graphic != null) { selItems.Remove(graphic); } ItemsSource.RemoveFromDataSource(idxInItemsSource, objectType); // RemoveFromDataSource() method causes first item in the ItemsSource to be selected when there were no // items selected in the data grid. We should avoid this selection by removing it from the selection: if (selCount == 0 && SelectedItems.Count == 1) { SelectedItems.Clear(); } graphic.AttributeValueChanged -= Graphic_AttributeValueChanged; SelectedGraphics.Remove(graphic); } } RestorePreviousSelection(selItems); } } else { // If exiting ItemsSource is not a PagedCollectionView then it was empty before // just populate it with everything in GraphicsLayer. SetItemsSource((GraphicsLayer != null && GraphicsLayer.Graphics != null) ? (IList <Graphic>)GraphicsLayer.Graphics : new List <Graphic>()); } } ShowNumberOfRecords(); }
/// <summary> /// Get a row from the FeatureDataGrid and returns the matching /// Graphic for the row. /// </summary> /// <param name="row">Row object from FeatureDataGrid.ItemsSource collection</param> /// <returns>Graphic that represented by a row object</returns> public Graphic GetGraphicFromRow(object row) { return(DataSourceCreator.GetGraphicSibling(row)); }
private bool AreEqual(object item, Graphic graphic) { return(graphic.Equals(DataSourceCreator.GetGraphicSibling(item))); }