/// <summary> /// This command adds our example "tag" attached property on all of the selected elements /// </summary> public static void OnTagSelection(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site) { var selectedModels = site.ActiveSelection.Select(vm => vm.Model).OfType <Element>(); if (!selectedModels.Any()) { return; } // When setting a tag we are modifying the model so we must set the tags in a transaction. Since all of the models are in // the same file we can set all of the tags in a single transaction. using (var transaction = selectedModels.First().TransactionManager.BeginTransaction("Tag Selection", TransactionPurpose.User)) { foreach (var element in selectedModels) { ExampleAttachedProperties.SetTag(element, "Tagged"); } transaction.Commit(); } }
/// <summary> /// This command finds all current model elements that have the example "tag" set and highlights the first element /// </summary> public static void OnFindTaggedElements(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site) { var rootElement = site.ActiveDocumentEditor?.EditorInfo?.RootElement; if (rootElement != null) { List <Element> taggedElements = new List <Element>(); foreach (var element in rootElement.GetSelfAndDescendantsBreadthFirst((e) => true)) { if (ExampleAttachedProperties.GetTag(element) == "Tagged") { taggedElements.Add(element); } } if (taggedElements.Any()) { // If we found tagged elements highlight all of them with a slight delay for a fun effect. // Note the usage of .IgnoreAwait(). This is a convention we use to ensure that any unhandled exceptions // are dealt with. SlowlyHighlightElementsAsync(site, taggedElements).IgnoreAwait(); } } }