private async void OnGraphicsElementSelectionChanged(ElementSelectionChangedEventArgs obj) { if (!(SelectedLayer is GraphicsLayer)) { return; } //check the container is a graphics layer - could be a Layout (or even map view) if (obj.ElementContainer is GraphicsLayer graphicsLayer) { //get the total selection count for the container var count = obj.SelectedElementCount; //Check count - could have been an unselect or clearselect if (count > 0) { //this is a selection or add to selection var selectedElements = graphicsLayer.GetSelectedElements().ToList(); var selectedGraphicElements = selectedElements.ConvertAll(x => (GraphicElement)x); //Get the first graphic to compare against var firstGraphicElement = selectedGraphicElements.FirstOrDefault(); await QueuedTask.Run(() => { //Get type of the first element var firstGraphicElementType = firstGraphicElement.GetGraphic().GetType(); //This needs to run on MCT. //iterate to check if elements match in the selectedElements collection var differentGraphicTypes = selectedGraphicElements.FirstOrDefault(se => se.GetGraphic().GetType() != firstGraphicElementType); _isAllGraphicsSameType = differentGraphicTypes == null ? true : false; System.Diagnostics.Debug.WriteLine($"No. of graphics selected: {count} Are they the same type? {_isAllGraphicsSameType}"); }); await GetStyleItemForGraphicsLayerAsync(SelectedLayer as GraphicsLayer); } } }
/// <summary> /// Event handler for graphics element changed event /// </summary> /// <param name="obj"></param> /// <remarks> /// When graphics element selection changes, a dictionary is updated with the Graphics Layer as the key and the elements in the layer that are currently selected as the values. /// </remarks> private async void OnElementSelectionChanged(ElementSelectionChangedEventArgs obj) { System.Diagnostics.Debug.WriteLine($"Debug Message: OnElementSelectionChanged"); if (obj.ElementContainer == null) { //skip – this is viewer initialization System.Diagnostics.Debug.WriteLine($"This is viewer initialization"); return; } if (obj.ElementContainer != null) { System.Diagnostics.Debug.WriteLine($"ElementContainer is not null"); var gl = obj.ElementContainer as GraphicsLayer; if (gl is GraphicsLayer) { System.Diagnostics.Debug.WriteLine($"gl is GraphicsLayer: {gl.GetType().Name}"); System.Diagnostics.Debug.WriteLine($"Selected Elements count: {obj.SelectedElementCount}"); if (GraphicsLayerSelectedElements.ContainsKey(gl)) //gl exists in the dictionary so only update value { GraphicsLayerSelectedElements[gl] = gl.GetSelectedElements().ToList(); System.Diagnostics.Debug.WriteLine($"Elements selected in {gl}. Collection already contained {gl}"); } else //doesn't exist, add it. { GraphicsLayerSelectedElements.Add(gl, gl.GetSelectedElements().ToList()); System.Diagnostics.Debug.WriteLine($"Elements selected in {gl}. Collection did NOT contain {gl}"); } //Weed out the layers with 0 selected items and maintain list GLWithElements.Clear(); foreach (var kvp in GraphicsLayerSelectedElements) { if (kvp.Value.Count > 0) //Layers with some selected items. { System.Diagnostics.Debug.WriteLine($"Weed out layers with no elements. Layer: {kvp.Key}. Elements {kvp.Value}"); GLWithElements.Add(kvp.Key, kvp.Value); } } //Analyze the GraphicsLayerSelectedElements dictionary to determine state var canMoveBackward = await CanSendToBack(); var canBringForward = await CanBringForward(); //set state for ordering SetState("can_send_backward_state", canMoveBackward); SetState("can_bring_forward_state", canBringForward); //set state for Grouping/Ungrouping SetState("can_group_graphics_state", CanGroupElements()); SetState("can_ungroup_graphics_state", CanUnGroupElements()); } } }
async public void LayoutSelectionCallBack(ElementSelectionChangedEventArgs args) { if (Globals.selEvents) { LayoutView layoutView = LayoutView.Active; Layout layout = layoutView.Layout; TextElement txtElm = layout.FindElement("Instructions") as TextElement; TextProperties txtProp = txtElm.TextProperties; TextElement statusText = layout.FindElement("Status") as TextElement; TextProperties statusProp = statusText.TextProperties; if (!(args.ElementContainer is Layout theLayoutView)) { return; } var selElm = theLayoutView.GetSelectedElements().ToList().FirstOrDefault(); // could also use layoutView.GetSelectedElements().FirstOrDefault(); if (Globals.elmType == "MF" && selElm is MapFrame) //Select appropriate Map Frame { txtProp.Text = "<bol>Instructions:</bol> \n\n - Select the rectangle where the map frame should be placed."; await QueuedTask.Run(() => txtElm.SetTextProperties(txtProp)); Globals.elmType = "REC"; Globals.mf_Name = selElm.Name; return; } if (Globals.elmType == "MF" && selElm is GraphicElement) { System.Windows.MessageBox.Show("Hey Bonehead: will you please follow instuctions, you selected the wrong thing!"); return; } else if (Globals.elmType == "REC" && selElm is GraphicElement) //Select appropriate Rectangle { txtProp.Text = "<bol>Instructions: </bol> \n\n - Select another map frame."; await QueuedTask.Run(() => txtElm.SetTextProperties(txtProp)); Globals.elmType = "MF"; Globals.i_guesses = Globals.i_guesses + 1; if (selElm.Name == "Rectangle 1" && Globals.mf_Name == "MF1") { MoveMF(selElm.Name); } else if (selElm.Name == "Rectangle 2" && Globals.mf_Name == "MF2") { MoveMF(selElm.Name); } else if (selElm.Name == "Rectangle 3" && Globals.mf_Name == "MF3") { MoveMF(selElm.Name); } else if (selElm.Name == "Rectangle 4" && Globals.mf_Name == "MF4") { MoveMF(selElm.Name); } else if (selElm.Name == "Rectangle 5" && Globals.mf_Name == "MF5") { MoveMF(selElm.Name); } else if (selElm.Name == "Rectangle 6" && Globals.mf_Name == "MF6") { MoveMF(selElm.Name); } else { statusProp.Text = "<bol><clr red='255'> WRONG!!! </clr></bol> <_bol> Try again. You are not very 'spatial'</_bol>"; await QueuedTask.Run(() => statusText.SetTextProperties(statusProp)); } return; } if (Globals.elmType == "REC" && selElm is MapFrame) { System.Windows.MessageBox.Show("Hey Bonehead: will you please follow instuctions, you selected the wrong thing!"); return; } } }