%3CLINGO-SUB%20id%3D%22lingo-sub-1445405%22%20slang%3D%22en-US%22%3EXAML%20Shapes%20manipulation%20level%20up%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1445405%22%20slang%3D%22en-US%22%3E%3CDIV%20id%3D%22MainContent%22%3E%0A%3CP%3EIn%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Ffree-your-mind-start-manipulating-xaml-shapes%2Fba-p%2F1442899%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eprevious%20article%3C%2FA%3E%2C%20we%20were%20able%20to%20provide%20a%20drag%20%26amp%3B%20drop%20functionality%20for%20moving%20XAML%20Shapes.%20We%20go%20further%3A%20Let's%20give%20the%20ability%20to%20the%20user%20to%20move%20the%20origin%20or%20the%20end%20of%20the%20line%20with%20an%20anchor.%20This%20way%2C%20we%20would%20be%20able%20to%20change%20the%20size%2C%20the%20orientation%2C%20the%20connections%20of%20the%20lines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22the-baseline-code%22%20id%3D%22toc-hId--1326732040%22%20id%3D%22toc-hId--1326732040%22%3E%3CA%20id%3D%22pragma-line-4%22%20target%3D%22_blank%22%3E%3C%2FA%3EThe%20baseline%20code%3C%2FH2%3E%0A%3CP%3EThe%20starting%20point%3F%20The%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fsbovo%2FUWP-Advanced-Inking%2Ftree%2Fmaster%2FStrokesToShapes%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eexact%20same%20as%20usual%2C%3C%2FA%3E%20related%20to%20the%20article%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Fuse-the-uwp-inking-platform-as-input-for-advanced-scenarios%2Fba-p%2F1352471%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EUse%20the%20UWP%20Inking%20platform%20as%20input%20for%20advanced%20scenarios%3C%2FA%3E.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-8%22%20class%3D%22language-xml%20hljs%22%3E%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EGrid%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EInkCanvas%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22inkCanvas%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%3C!--%20Canvas%20for%20displaying%20the%20%22recognized%22%20XAML%20Shapes%20--%3E%3C%2FSPAN%3E%0A%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ECanvas%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22ShapesCanvas%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22hljs-tag%22%3EGrid%3C%2FSPAN%3E%26gt%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-17%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EMainPage%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ethis%3C%2FSPAN%3E.InitializeComponent()%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Initialize%20the%20InkCanvas%3C%2FSPAN%3E%0A%20inkCanvas.InkPresenter.InputDeviceTypes%20%3D%0A%20%20Windows.UI.Core.CoreInputDeviceTypes.Mouse%20%7C%0A%20%20Windows.UI.Core.CoreInputDeviceTypes.Pen%20%7C%0A%20%20Windows.UI.Core.CoreInputDeviceTypes.Touch%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20When%20the%20user%20finished%20to%20draw%20something%20on%20the%20InkCanvas%3C%2FSPAN%3E%0A%20inkCanvas.InkPresenter.StrokesCollected%20%2B%3D%20InkPresenter_StrokesCollected%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EInkPresenter_StrokesCollected%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%0A%20Windows.UI.Input.Inking.InkPresenter%20sender%2C%0A%20Windows.UI.Input.Inking.InkStrokesCollectedEventArgs%20args%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20InkStroke%20stroke%20%3D%20inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Last()%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Action%201%20%3D%20We%20use%20a%20function%20that%20we%20will%20implement%20just%20after%20to%20create%20the%20XAML%20Line%3C%2FSPAN%3E%0A%20Line%20line%20%3D%20ConvertStrokeToXAMLLine(stroke)%3B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Action%202%20%3D%20We%20add%20the%20Line%20in%20the%20second%20Canvas%3C%2FSPAN%3E%0A%20ShapesCanvas.Children.Add(line)%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20We%20delete%20the%20InkStroke%20from%20the%20InkCanvas%3C%2FSPAN%3E%0A%20stroke.Selected%20%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Etrue%3C%2FSPAN%3E%3B%0A%20inkCanvas.InkPresenter.StrokeContainer.DeleteSelected()%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20Line%20%3CSPAN%20class%3D%22hljs-title%22%3EConvertStrokeToXAMLLine%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3EInkStroke%20stroke%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20line%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Line()%3B%0A%20line.Stroke%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.Green)%3B%0A%20line.StrokeThickness%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E6%3C%2FSPAN%3E%3B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20The%20origin%20%3D%20(X1%2C%20Y1)%3C%2FSPAN%3E%0A%20line.X1%20%3D%20stroke.GetInkPoints().First().Position.X%3B%0A%20line.Y1%20%3D%20stroke.GetInkPoints().First().Position.Y%3B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20The%20end%20%3D%20(X2%2C%20Y2)%3C%2FSPAN%3E%0A%20line.X2%20%3D%20stroke.GetInkPoints().Last().Position.X%3B%0A%20line.Y2%20%3D%20stroke.GetInkPoints().Last().Position.Y%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ereturn%3C%2FSPAN%3E%20line%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22adding-the-anchors%22%20id%3D%22toc-hId-1160780793%22%20id%3D%22toc-hId-1160780793%22%3E%3CA%20id%3D%22pragma-line-65%22%20target%3D%22_blank%22%3E%3C%2FA%3EAdding%20the%20anchors%3C%2FH2%3E%0A%3CP%3EIn%20the%20%3CEM%3EConvertStrokeToXAMLLine%3C%2FEM%3E%20method%2C%20just%20before%20returning%20the%20%3CSTRONG%3Eline%3C%2FSTRONG%3E%20object%2C%20we%20add%20a%20handler%20for%20the%20%3CSTRONG%3ETapped%3C%2FSTRONG%3E%20event%20which%20is%20raised%20no%20matter%20if%20we%20use%20the%20touch%2C%20mouse%20or%20pen.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-68%22%20class%3D%22language-csharp%20hljs%22%3Eline.Tapped%20%2B%3D%20Line_Tapped%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20%3CEM%3ELine_Tapped%3C%2FEM%3E%20will%20be%20the%20place%20to%20draw%20the%20anchors%20on%20the%20lines%20ends%20and%20initiate%20the%20handlers%20for%20allowing%20the%20drag%20%26amp%3B%20drop%20actions%20on%20these%20anchors.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%221-get-the-line--some-cosmetics%22%20id%3D%22toc-hId-1851342267%22%20id%3D%22toc-hId-1851342267%22%3E%3CA%20id%3D%22pragma-line-74%22%20target%3D%22_blank%22%3E%3C%2FA%3E1.%20Get%20the%20line%20%2B%20some%20cosmetics%3C%2FH3%3E%0A%3CP%3EWe%20get%20the%20line%20tapped%20from%20the%20%3CSTRONG%3Esender%3C%2FSTRONG%3E%20object%20passed%20as%20parameter%20for%20the%20event%20handler.%20To%20visually%20show%20the%20selected%20line%2C%20we%20can%20modify%20the%20stroke%20or%20the%20color.%20Here%20is%20a%20sample%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-76%22%20class%3D%22language-csharp%20hljs%22%3ELine%20line%20%3D%20(Line)sender%3B%0Aline.Stroke%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.DarkRed)%3B%0Aline.StrokeThickness%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E10%3C%2FSPAN%3E%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%222-draw-the-origin-and-the-end-of-the-line%22%20id%3D%22toc-hId-43887804%22%20id%3D%22toc-hId-43887804%22%3E%3CA%20id%3D%22pragma-line-82%22%20target%3D%22_blank%22%3E%3C%2FA%3E2.%20Draw%20the%20origin%20and%20the%20end%20of%20the%20line%3C%2FH3%3E%0A%3CP%3ETo%20display%20an%20anchor%20at%20the%20ends%20of%20the%20lines%2C%20we%20draw%20a%20circle%20by%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Shapes.Ellipse%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EXAML%20Ellipse%20class%3C%2FA%3E%20with%20the%20same%20Height%20and%20Width.%20We%20create%202%20circles%3A%20%3CEM%3EanchorOrigin%3C%2FEM%3E%20and%20%3CEM%3EanchorEnd%3C%2FEM%3E%20for%20respectively%20the%20origin%20and%20the%20end%20of%20the%20line.%20We%20add%20these%20circles%20to%20the%20canvas%20with%20%3CCODE%3EShapesCanvas.Children.Add(MyObject)%3C%2FCODE%3E.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-84%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eint%3C%2FSPAN%3E%20size_EndLines%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E25%3C%2FSPAN%3E%3B%0A%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Create%202%20circles%20for%20the%20ends%20of%20the%20line%3C%2FSPAN%3E%0AEllipse%20anchorOrigin%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Ellipse%0A%7B%0A%20%20%20Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.OrangeRed)%2C%0A%20%20%20Height%20%3D%20size_EndLines%2C%0A%20%20%20Width%20%3D%20size_EndLines%0A%7D%3B%0AShapesCanvas.Children.Add(anchorOrigin)%3B%0A%0AEllipse%20anchorEnd%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Ellipse%0A%7B%0A%20%20%20Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.OrangeRed)%2C%0A%20%20%20Height%20%3D%20size_EndLines%2C%0A%20%20%20Width%20%3D%20size_EndLines%0A%7D%3B%0AShapesCanvas.Children.Add(anchorEnd)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20set%20the%20position%20of%20each%20circle%20in%20order%20that%20its%20center%20matches%20the%20position%20of%20the%20end%20of%20the%20line.%20For%20this%2C%20we%20use%20two%20functions%20to%20modify%20the%20%3CSTRONG%3ECanvas.Left%3C%2FSTRONG%3E%20and%20%3CSTRONG%3ECanvas.Top%3C%2FSTRONG%3E%20attached%20properties%20of%20the%20circles%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-105%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Put%20the%20anchors%20at%20the%20origin%20and%20at%20the%20end%20of%20the%20line%3C%2FSPAN%3E%0ACanvas.SetLeft(anchorOrigin%2C%20line.X1%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0ACanvas.SetLeft(anchorEnd%2C%20line.X2%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0ACanvas.SetTop(anchorOrigin%2C%20line.Y1%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0ACanvas.SetTop(anchorEnd%2C%20line.Y2%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%223-add-the-event-handlers-for-the-circles%22%20id%3D%22toc-hId--1763566659%22%20id%3D%22toc-hId--1763566659%22%3E%3CA%20id%3D%22pragma-line-113%22%20target%3D%22_blank%22%3E%3C%2FA%3E3.%20Add%20the%20event%20handlers%20for%20the%20circles%3C%2FH3%3E%0A%3CP%3EThe%20strategy%20here%20is%20the%20exact%20same%20as%20described%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Ffree-your-mind-start-manipulating-xaml-shapes%2Fba-p%2F1442899%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eprevious%20article%3C%2FA%3E%20with%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Fwindows%2Fapps%2Fhh465387(v%3Dwin.10)%23using-manipulation-events%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulation%20events%3C%2FA%3E%20applied%20to%20both%20anchors%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-115%22%3E%0A%3CLI%20id%3D%22pragma-line-115%22%3EThe%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationmode%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulationMode%3C%2FA%3E%3C%2FSTRONG%3E%20property%20can%20be%20used%20to%20restraint%20the%20move%20of%20the%20shape%20in%20only%20on%20direction.%20In%20our%20sample%2C%20we%20would%20like%20to%20move%20on%20both%20axis%20X%20and%20Y.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-116%22%3EThe%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationstarted%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulationStarted%3C%2FA%3E%3C%2FSTRONG%3E%20event%20is%20raised%20when%20we%20start%20the%20drag%20%26amp%3B%20drop.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-117%22%3EThe%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationdelta%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulationDelta%3C%2FA%3E%3C%2FSTRONG%3E%20event%20gives%20the%20possibility%20to%20give%20visual%20feedback%20for%20every%20move%20during%20the%20drag%20%26amp%3B%20drop.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-118%22%3EThe%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationcompleted%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulationCompleted%3C%2FA%3E%3C%2FSTRONG%3E%20event%20occurs%20when%20the%20drag%20%26amp%3B%20drop%20is%20finished.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CBLOCKQUOTE%20id%3D%22pragma-line-121%22%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20For%20the%20%3CEM%3EManipulationDelta%3C%2FEM%3E%20and%20%3CEM%3EManipulationCompleted%3C%2FEM%3E%20events%2C%20we%20are%20forced%20to%20use%20two%20different%20handlers%20because%20the%20code%20is%20modifying%20the%20position%20of%20the%20ends%20of%20the%20lines%20and%20we%20have%20to%20know%20which%20end%20(origin%20or%20end)%20to%20modify.%20The%20handler%20for%20the%20%3CEM%3EManipulationStarted%3C%2FEM%3E%20event%20is%20the%20same%20for%20both%20anchors.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-123%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Enable%20manipulations%20on%20the%20anchors%3C%2FSPAN%3E%0AanchorOrigin.ManipulationMode%20%3D%20ManipulationModes.TranslateX%20%7C%20ManipulationModes.TranslateY%3B%0AanchorOrigin.ManipulationStarted%20%2B%3D%20Anchor_ManipulationStarted%3B%0AanchorOrigin.ManipulationDelta%20%2B%3D%20Anchor_Origin_ManipulationDelta%3B%0AanchorOrigin.ManipulationCompleted%20%2B%3D%20Anchor_Origin_ManipulationCompleted%3B%0A%0AanchorEnd.ManipulationMode%20%3D%20ManipulationModes.TranslateX%20%7C%20ManipulationModes.TranslateY%3B%0AanchorEnd.ManipulationStarted%20%2B%3D%20Anchor_ManipulationStarted%3B%0AanchorEnd.ManipulationDelta%20%2B%3D%20Anchor_End_ManipulationDelta%3B%0AanchorEnd.ManipulationCompleted%20%2B%3D%20Anchor_End_ManipulationCompleted%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%224-keep-track-of-the-selected-line%22%20id%3D%22toc-hId-723946174%22%20id%3D%22toc-hId-723946174%22%3E%3CA%20id%3D%22pragma-line-136%22%20target%3D%22_blank%22%3E%3C%2FA%3E4.%20Keep%20track%20of%20the%20selected%20line%3C%2FH3%3E%0A%3CP%3EWe%20need%20to%20store%20some%20object%20to%20make%20the%20drag%20%26amp%3B%20drop%20working%20and%20to%20be%20also%20able%20to%20'unselect'%20and%20'select'%20a%20new%20line.%20Let%20me%20explain%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-138%22%3E%0A%3CLI%20id%3D%22pragma-line-138%22%3EThe%20line%20and%20its%20initial%20coordinates%20(x1%2Cy1)%20-%20(x2%2Cy2)%20are%20necessary%20to%20finalize%20the%20move%20when%20the%20drag%20%26amp%3B%20drop%20is%20complete.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-139%22%3EThe%20anchors%20of%20the%20line%20have%20to%20be%20deleted%20when%20the%20user%20click%20or%20touch%20another%20line.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ETo%20do%20this%2C%20we%20add%20a%20structure%20and%20we%20use%20it%20as%20a%20field%20that%20can%20be%20accessible%20by%20the%20code%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-142%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estruct%3C%2FSPAN%3E%20ActiveLine%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20Line%20line%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20initialX1%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20initialY1%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20initialX2%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20initialY2%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20Ellipse%20AnchorOrigin%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20Ellipse%20AnchorEnd%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20ActiveLine%20activeLine%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThen%2C%20we%20create%20method%20to%20do%20the%20initialization%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-158%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EInitializeActiveLine%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3ELine%20line%2C%20Ellipse%20origin%2C%20Ellipse%20end%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%20%20activeLine.line%20%3D%20line%3B%0A%20%20%20activeLine.initialX1%20%3D%20line.X1%3B%0A%20%20%20activeLine.initialY1%20%3D%20line.Y1%3B%0A%20%20%20activeLine.initialX2%20%3D%20line.X2%3B%0A%20%20%20activeLine.initialY2%20%3D%20line.Y2%3B%0A%20%20%20activeLine.AnchorOrigin%20%3D%20origin%3B%0A%20%20%20activeLine.AnchorEnd%20%3D%20end%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20use%20this%20method%20as%20the%20last%20instruction%20of%20the%20%3CEM%3ELine_Tapped%3C%2FEM%3E%20event%20handler%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-172%22%20class%3D%22language-csharp%20hljs%22%3EInitializeActiveLine(line%2C%20anchorOrigin%2C%20anchorEnd)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%225-unselect-the-line%22%20id%3D%22toc-hId--1083508289%22%20id%3D%22toc-hId--1083508289%22%3E%3CA%20id%3D%22pragma-line-177%22%20target%3D%22_blank%22%3E%3C%2FA%3E5.%20Unselect%20the%20line%3C%2FH3%3E%0A%3CP%3EThis%20step%20is%2C%20in%20fact%20the%20very%20first%20we%20have%20to%20do%20while%20entering%20to%20the%20%3CEM%3ELine_Tapped%3C%2FEM%3E%20event%20handler%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-180%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ELine_Tapped%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%20TappedRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Remove%20the%20anchor%20from%20the%20selected%20line%3C%2FSPAN%3E%0A%20UnselectActiveLine()%3B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20...%3C%2FSPAN%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20code%20of%20%3CEM%3EUnselectActiveLine%3C%2FEM%3E%20just%20put%20back%20the%20original%20color%2Fstroke%20of%20the%20line%20and%20more%20important%3A%20delete%20the%20anchors%20for%20this%20line%20because%20we%20would%20like%20to%20have%20the%20anchors%20only%20for%20the%20lines%20we%20select.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-189%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EUnselectActiveLine%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(activeLine.line%20!%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E%20%0A%20%20%26amp%3B%26amp%3B%20activeLine.AnchorOrigin!%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E%0A%20%20%26amp%3B%26amp%3B%20activeLine.AnchorEnd%20!%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E)%0A%20%7B%0A%20%20activeLine.line.Stroke%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.Green)%3B%0A%20%20activeLine.line.StrokeThickness%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E6%3C%2FSPAN%3E%3B%0A%20%20ShapesCanvas.Children.Remove(activeLine.AnchorOrigin)%3B%0A%20%20ShapesCanvas.Children.Remove(activeLine.AnchorEnd)%3B%0A%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CBLOCKQUOTE%20id%3D%22pragma-line-204%22%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20This%20'unselection'%20is%20the%20way%20we%20chose%20for%20our%20code.%20The%20other%20possibility%20would%20be%20to%20use%20a%20'switch%20to%20modification%20mode'%20button%20on%20the%20UI%20in%20order%20to%20go%20to%20a%20different%20mode%3A%20We%20would%20deactivate%20drawing%20on%20the%20InkCanvas%20and%20we%20could%20create%2Fdisplay%20all%20anchors%20of%20all%20lines%20to%20allow%20modifications.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%226-all-pieces-of-the-line_tapped-event-handler%22%20id%3D%22toc-hId-1404004544%22%20id%3D%22toc-hId-1404004544%22%3E%3CA%20id%3D%22pragma-line-207%22%20target%3D%22_blank%22%3E%3C%2FA%3E6.%20All%20pieces%20of%20the%20%3CEM%3ELine_Tapped%3C%2FEM%3E%20event%20handler%3C%2FH3%3E%0A%3CP%3ELet's%20put%20together%20all%20we%20just%20explained.%20Here%20is%20the%20final%20code%20of%20the%20%3CEM%3ELine_Tapped%3C%2FEM%3E%20method%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-210%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ELine_Tapped%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%20TappedRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Remove%20the%20anchor%20from%20the%20selected%20line%3C%2FSPAN%3E%0A%20UnselectActiveLine()%3B%0A%0A%20Line%20line%20%3D%20(Line)sender%3B%0A%20line.Stroke%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.DarkRed)%3B%0A%20line.StrokeThickness%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E10%3C%2FSPAN%3E%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eint%3C%2FSPAN%3E%20size_EndLines%20%3D%20%3CSPAN%20class%3D%22hljs-number%22%3E25%3C%2FSPAN%3E%3B%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Create%202%20circles%20for%20the%20ends%20of%20the%20line%3C%2FSPAN%3E%0A%20Ellipse%20anchorOrigin%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Ellipse%0A%20%7B%0A%20%20Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.OrangeRed)%2C%0A%20%20Height%20%3D%20size_EndLines%2C%0A%20%20Width%20%3D%20size_EndLines%0A%20%7D%3B%0A%20ShapesCanvas.Children.Add(anchorOrigin)%3B%0A%0A%20Ellipse%20anchorEnd%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Ellipse%0A%20%7B%0A%20%20Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.OrangeRed)%2C%0A%20%20Height%20%3D%20size_EndLines%2C%0A%20%20Width%20%3D%20size_EndLines%0A%20%7D%3B%0A%20ShapesCanvas.Children.Add(anchorEnd)%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Put%20the%20anchors%20at%20the%20origin%20and%20at%20the%20end%20of%20the%20line%3C%2FSPAN%3E%0A%20Canvas.SetLeft(anchorOrigin%2C%20line.X1%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0A%20Canvas.SetLeft(anchorEnd%2C%20line.X2%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0A%20Canvas.SetTop(anchorOrigin%2C%20line.Y1%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0A%20Canvas.SetTop(anchorEnd%2C%20line.Y2%20-%20size_EndLines%20%2F%20%3CSPAN%20class%3D%22hljs-number%22%3E2%3C%2FSPAN%3E)%3B%0A%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Enable%20manipulations%20on%20the%20anchors%3C%2FSPAN%3E%0A%20anchorOrigin.ManipulationMode%20%3D%20ManipulationModes.TranslateX%20%7C%20ManipulationModes.TranslateY%3B%0A%20anchorOrigin.ManipulationStarted%20%2B%3D%20Anchor_ManipulationStarted%3B%0A%20anchorOrigin.ManipulationDelta%20%2B%3D%20Anchor_Origin_ManipulationDelta%3B%0A%20anchorOrigin.ManipulationCompleted%20%2B%3D%20Anchor_Origin_ManipulationCompleted%3B%0A%0A%20anchorEnd.ManipulationMode%20%3D%20ManipulationModes.TranslateX%20%7C%20ManipulationModes.TranslateY%3B%0A%20anchorEnd.ManipulationStarted%20%2B%3D%20Anchor_ManipulationStarted%3B%0A%20anchorEnd.ManipulationDelta%20%2B%3D%20Anchor_End_ManipulationDelta%3B%0A%20anchorEnd.ManipulationCompleted%20%2B%3D%20Anchor_End_ManipulationCompleted%3B%0A%0A%20InitializeActiveLine(line%2C%20anchorOrigin%2C%20anchorEnd)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22the-manipulations-events-for-the-anchors%22%20id%3D%22toc-hId-1393501440%22%20id%3D%22toc-hId-1393501440%22%3E%3CA%20id%3D%22pragma-line-260%22%20target%3D%22_blank%22%3E%3C%2FA%3EThe%20manipulations'%20events%20for%20the%20anchors%3C%2FH2%3E%0A%3CH3%20id%3D%22initiate-the-manipulation%22%20id%3D%22toc-hId-2084062914%22%20id%3D%22toc-hId-2084062914%22%3E%3CA%20id%3D%22pragma-line-262%22%20target%3D%22_blank%22%3E%3C%2FA%3EInitiate%20the%20manipulation%3C%2FH3%3E%0A%3CP%3EAs%20described%20and%20explained%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Ffree-your-mind-start-manipulating-xaml-shapes%2Fba-p%2F1442899%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eprevious%20article%3C%2FA%3E%2C%20we%20instantiate%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Media.TranslateTransform%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ETranslateTransform%3C%2FA%3E%20object%20(named%20%3CSTRONG%3EdragTranslation%3C%2FSTRONG%3E)%20and%20we%20affect%20it%20to%20the%20%3CSTRONG%3ERenderTransform%3C%2FSTRONG%3E%20property%20of%20the%20anchor.%20The%20goal%20is%20to%20apply%20a%20translation%20for%20every%20move%20during%20the%20drag%20%26amp%3B%20drop%20action.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-266%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20XAML%20Shapes%20manipulations%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20TranslateTransform%20dragTranslation%3B%0A%20%20%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchor_ManipulationStarted%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationStartedRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20Ellipse%20anchor%20%3D%20(Ellipse)sender%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Initialize%20the%20transforms%20that%20will%20be%20used%20to%20manipulate%20the%20shape%3C%2FSPAN%3E%0A%20dragTranslation%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20TranslateTransform()%3B%0A%20anchor.RenderTransform%20%3D%20dragTranslation%3B%0A%20anchor.Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.Orange)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22provide-visual-feedback%22%20id%3D%22toc-hId--418555028%22%20id%3D%22toc-hId--418555028%22%3E%3CA%20id%3D%22pragma-line-282%22%20target%3D%22_blank%22%3E%3C%2FA%3EProvide%20visual%20feedback%3C%2FH3%3E%0A%3CP%3EThe%20handler%20for%20the%20%3CEM%3EManipulationDelta%3C%2FEM%3E%20has%20to%20know%20if%20we%20are%20handling%2Fmoving%20the%20origin%20or%20the%20end%20of%20the%20line%20in%20order%20to%20be%20able%20to%20move%20the%20line%20correctly.%20That%20is%20why%20we%20have%20two%20different%20handlers%20but%20calling%20the%20same%20sub%20method.%20The%20third%20parameter%20is%20a%20boolean%20indicating%20if%20we%20hare%20modifying%20the%20origin%20(true)%20or%20not%20(false)%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-285%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchor_Origin_ManipulationDelta%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationDeltaRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20AnchorManipulationDelta(sender%2C%20e%2C%20%3CSPAN%20class%3D%22hljs-literal%22%3Etrue%3C%2FSPAN%3E)%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchor_End_ManipulationDelta%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationDeltaRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20AnchorManipulationDelta(sender%2C%20e%2C%20%3CSPAN%20class%3D%22hljs-literal%22%3Efalse%3C%2FSPAN%3E)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESo%2C%20in%20the%20%3CEM%3EAnchorManipulationDelta%3C%2FEM%3E%20method%2C%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-300%22%3E%0A%3CLI%20id%3D%22pragma-line-300%22%3EWe%20first%20modify%20the%20%3CSTRONG%3EdragTranslation%3C%2FSTRONG%3E%20which%20acts%20on%20the%20anchor%20moves.%20This%20code%20effectively%20change%20the%20coordinates%20x%20and%20y%20of%20the%20anchor%20corresponding%20to%20the%20move%20of%20the%20mouse%2C%20pen%20or%20mouse.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-301%22%3EAfter%2C%20we%20changes%20accordingly%20the%20position%20of%20either%20the%20origin%20or%20the%20end%20of%20the%20line.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-302%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchorManipulationDelta%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationDeltaRoutedEventArgs%20e%2C%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ebool%3C%2FSPAN%3E%20OriginofLine%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20x%20%3D%20e.Delta.Translation.X%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20y%20%3D%20e.Delta.Translation.Y%3B%0A%20dragTranslation.X%20%2B%3D%20x%3B%0A%20dragTranslation.Y%20%2B%3D%20y%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(OriginofLine)%0A%20%7B%0A%20%20activeLine.line.X1%20%2B%3D%20x%3B%0A%20%20activeLine.line.Y1%20%2B%3D%20y%3B%0A%20%7D%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eelse%3C%2FSPAN%3E%0A%20%7B%0A%20%20activeLine.line.X2%20%2B%3D%20x%3B%0A%20%20activeLine.line.Y2%20%2B%3D%20y%3B%0A%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22finish-the-manipulation%22%20id%3D%22toc-hId-2068957805%22%20id%3D%22toc-hId-2068957805%22%3E%3CA%20id%3D%22pragma-line-326%22%20target%3D%22_blank%22%3E%3C%2FA%3EFinish%20the%20manipulation%3C%2FH3%3E%0A%3CP%3EThe%20same%20hack%20is%20used%20to%20identify%20if%20we%20did%20a%20move%20on%20the%20origin%20or%20on%20the%20end%20of%20the%20line.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-328%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchor_Origin_ManipulationCompleted%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationCompletedRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20AnchorManipulationCompleted(sender%2C%20e%2C%20%3CSPAN%20class%3D%22hljs-literal%22%3Etrue%3C%2FSPAN%3E)%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchor_End_ManipulationCompleted%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationCompletedRoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20AnchorManipulationCompleted(sender%2C%20e%2C%20%3CSPAN%20class%3D%22hljs-literal%22%3Efalse%3C%2FSPAN%3E)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20actions%20we%20have%20to%20perform%20for%20terminating%20the%20drag%20%26amp%3B%20drop%20are%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-343%22%3E%0A%3CLI%20id%3D%22pragma-line-343%22%3E'Resetting'%20the%20translation%20applied%20to%20the%20anchor%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-344%22%3EGetting%20the%20total%20translation%20on%20the%20X%20axis%20and%20Y%20axis%20with%20the%20parameter%20%3CSTRONG%3EManipulationCompletedRoutedEventArgs.Cumulative.Translation%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-345%22%3EAdding%20this%20translation%20to%20the%20original%20coordinates%20of%20either%20the%20origin%20or%20the%20end%20of%20the%20line%20in%20order%20to%20modify%20definitely%20its%20position.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-346%22%3EAffecting%20the%20new%20position%20of%20the%20end%20of%20the%20line%20to%20the%20field%20%3CEM%3EactiveLine%3C%2FEM%3E%20that%20tracks%20the%20selected%20line.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-348%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAnchorManipulationCompleted%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%0A%20ManipulationCompletedRoutedEventArgs%20e%2C%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ebool%3C%2FSPAN%3E%20OriginofLine%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20Ellipse%20anchor%20%3D%20(Ellipse)sender%3B%0A%20anchor.RenderTransform%20%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20x%20%3D%20e.Cumulative.Translation.X%3B%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Edouble%3C%2FSPAN%3E%20y%20%3D%20e.Cumulative.Translation.Y%3B%0A%20Canvas.SetLeft(anchor%2C%20Canvas.GetLeft(anchor)%20%2B%20x)%3B%0A%20Canvas.SetTop(anchor%2C%20Canvas.GetTop(anchor)%20%2B%20y)%3B%0A%0A%20anchor.Fill%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20SolidColorBrush(Windows.UI.Colors.Black)%3B%0A%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(OriginofLine)%0A%20%7B%0A%20%20activeLine.line.X1%20%3D%20activeLine.initialX1%20%2B%20x%3B%0A%20%20activeLine.line.Y1%20%3D%20activeLine.initialY1%20%2B%20y%3B%0A%0A%20%20activeLine.initialX1%20%3D%20activeLine.line.X1%3B%0A%20%20activeLine.initialY1%20%3D%20activeLine.line.Y1%3B%0A%20%7D%0A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eelse%3C%2FSPAN%3E%0A%20%7B%0A%20%20activeLine.line.X2%20%3D%20activeLine.initialX2%20%2B%20x%3B%0A%20%20activeLine.line.Y2%20%3D%20activeLine.initialY2%20%2B%20y%3B%0A%0A%20%20activeLine.initialX2%20%3D%20activeLine.line.X2%3B%0A%20%20activeLine.initialY2%20%3D%20activeLine.line.Y2%3B%0A%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHere%20we%20are!%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorSebastien%20Bovo_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22AdvancedManipulations.gif%22%20style%3D%22width%3A%20538px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F197028i3BAB744600FA15E0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22AdvancedManipulations.gif%22%20alt%3D%22Moving%20the%20origin%20or%20the%20end%20of%20the%20line%20with%20an%20anchor%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EMoving%20the%20origin%20or%20the%20end%20of%20the%20line%20with%20an%20anchor%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22wrapping-up%22%20id%3D%22toc-hId-132420623%22%20id%3D%22toc-hId-132420623%22%3E%3CA%20id%3D%22pragma-line-386%22%20target%3D%22_blank%22%3E%3C%2FA%3EWrapping%20up%3C%2FH2%3E%0A%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Fwindows%2Fapps%2Fhh465387(v%3Dwin.10)%23using-manipulation-events%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EManipulation%20events%3C%2FA%3E%20are%20really%20powerful%20and%20can%20be%20applied%20to%20any%20XAML%20Shapes.%20In%20this%20sample%2C%20we%20just%20create%20two%20circles%20like%20anchors%20at%20the%20ends%20of%20the%20line%20in%20order%20to%20give%20the%20ability%20to%20the%20user%20to%20manipulate%20the%20line.%20We%20could%20think%20of%20building%20a%20more%20sophisticated%20UI%20with%20different%20buttons%2Fshapes%20to%20apply%20translations%2C%20rotations%20or%20any%20actions%20on%20any%20other%20shapes%20of%20the%20application!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E--%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20source%20is%20available%20on%20GitHub%20-%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FWindows-AppConsult-Samples-UWP%2Ftree%2Fmaster%2FUWP-Advanced-Inking%2FXAMLShapesAdvancedManipulations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fsbovo%2FUWP-Advanced-Inking%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E-ERR%3AREF-NOT-FOUND-%40sbovo%20for%20the%20AppConsult%20team.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22inking-series-articles%22%20id%3D%22toc-hId--1675033840%22%20id%3D%22toc-hId--1675033840%22%3E%3CA%20id%3D%22pragma-line-397%22%20target%3D%22_blank%22%3E%3C%2FA%3EInking%20series'%20articles%3C%2FH2%3E%0A%3CP%3EThis%20article%20is%20part%20of%20a%20series%20exploring%20concepts%20about%20inking%20and%20XAML%20Shapes.%20Here%20are%20all%20links%3A%3C%2FP%3E%0A%3COL%20id%3D%22pragma-line-399%22%3E%0A%3CLI%20id%3D%22pragma-line-399%22%3E-ERR%3AREF-NOT-FOUND-Use%20the%20UWP%20Inking%20platform%20as%20input%20for%20advanced%20scenarios%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-400%22%3E-ERR%3AREF-NOT-FOUND-Handling%20zoom%20in%20Inking%20applications%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-401%22%3E-ERR%3AREF-NOT-FOUND-Turning%20to%20the%20dark%20side%20of%20inking%20%3D%20UnprocessedInput%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-402%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-dev-appconsult%2Ffree-your-mind-start-manipulating-xaml-shapes%2Fba-p%2F1442899%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EFree%20your%20mind%3A%20Start%20manipulating%20XAML%20Shapes%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-403%22%3E%3CSTRONG%3EXAML%20Shapes%20manipulation%20level%20up%3C%2FSTRONG%3E%20%E2%87%90%20You%20are%20here%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22references%22%20id%3D%22toc-hId-812478993%22%20id%3D%22toc-hId-812478993%22%3E%3CA%20id%3D%22pragma-line-405%22%20target%3D%22_blank%22%3E%3C%2FA%3EReferences%3C%2FH2%3E%0A%3CUL%20id%3D%22pragma-line-406%22%3E%0A%3CLI%20id%3D%22pragma-line-406%22%3ESource%20code%20of%20this%20article%20-%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FWindows-AppConsult-Samples-UWP%2Ftree%2Fmaster%2FUWP-Advanced-Inking%2FXAMLShapesAdvancedManipulations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fsbovo%2FUWP-Advanced-Inking%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-407%22%3EEllipse%20Class%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Shapes.Ellipse%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Shapes.Ellipse%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-408%22%3EManipulation%20events%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Fwindows%2Fapps%2Fhh465387(v%3Dwin.10)%23using-manipulation-events%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Fwindows%2Fapps%2Fhh465387(v%3Dwin.10)%23using-manipulation-events%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-409%22%3EManipulationMode%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationmode%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationmode%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-410%22%3EUIElement.ManipulationStarted%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationstarted%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationstarted%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-411%22%3EUIElement.ManipulationDelta%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationdelta%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationdelta%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-412%22%3EUIElement.ManipulationCompleted%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationcompleted%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2Fwindows.ui.xaml.uielement.manipulationcompleted%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-413%22%3ETranslateTransform%20-%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Media.TranslateTransform%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fuwp%2Fapi%2FWindows.UI.Xaml.Media.TranslateTransform%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1445405%22%20slang%3D%22en-US%22%3E%3CP%3ELet's%20give%20the%20ability%20to%20the%20user%20to%20move%20the%20origin%20or%20the%20end%20of%20the%20line%20with%20an%20anchor%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

In the previous article, we were able to provide a drag & drop functionality for moving XAML Shapes. We go further: Let's give the ability to the user to move the origin or the end of the line with an anchor. This way, we would be able to change the size, the orientation, the connections of the lines.

 

 

The baseline code

The starting point? The exact same as usual, related to the article Use the UWP Inking platform as input for advanced scenarios.

<Grid>
	<InkCanvas x:Name="inkCanvas" />

	<!-- Canvas for displaying the "recognized" XAML Shapes -->
	<Canvas x:Name="ShapesCanvas" />
</Grid>
public MainPage()
{
	this.InitializeComponent();

	// Initialize the InkCanvas
	inkCanvas.InkPresenter.InputDeviceTypes =
		Windows.UI.Core.CoreInputDeviceTypes.Mouse |
		Windows.UI.Core.CoreInputDeviceTypes.Pen |
		Windows.UI.Core.CoreInputDeviceTypes.Touch;

	// When the user finished to draw something on the InkCanvas
	inkCanvas.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;
}

private void InkPresenter_StrokesCollected(
	Windows.UI.Input.Inking.InkPresenter sender,
	Windows.UI.Input.Inking.InkStrokesCollectedEventArgs args)
{
	InkStroke stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Last();

	// Action 1 = We use a function that we will implement just after to create the XAML Line
	Line line = ConvertStrokeToXAMLLine(stroke);
	// Action 2 = We add the Line in the second Canvas
	ShapesCanvas.Children.Add(line);

	// We delete the InkStroke from the InkCanvas
	stroke.Selected = true;
	inkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
}

private Line ConvertStrokeToXAMLLine(InkStroke stroke)
{
	var line = new Line();
	line.Stroke = new SolidColorBrush(Windows.UI.Colors.Green);
	line.StrokeThickness = 6;
	// The origin = (X1, Y1)
	line.X1 = stroke.GetInkPoints().First().Position.X;
	line.Y1 = stroke.GetInkPoints().First().Position.Y;
	// The end = (X2, Y2)
	line.X2 = stroke.GetInkPoints().Last().Position.X;
	line.Y2 = stroke.GetInkPoints().Last().Position.Y;

	return line;
}

 

 

Adding the anchors

In the ConvertStrokeToXAMLLine method, just before returning the line object, we add a handler for the Tapped event which is raised no matter if we use the touch, mouse or pen.

line.Tapped += Line_Tapped;

The Line_Tapped will be the place to draw the anchors on the lines ends and initiate the handlers for allowing the drag & drop actions on these anchors.

 

1. Get the line + some cosmetics

We get the line tapped from the sender object passed as parameter for the event handler. To visually show the selected line, we can modify the stroke or the color. Here is a sample:

Line line = (Line)sender;
line.Stroke = new SolidColorBrush(Windows.UI.Colors.DarkRed);
line.StrokeThickness = 10;

 

2. Draw the origin and the end of the line

To display an anchor at the ends of the lines, we draw a circle by using the XAML Ellipse class with the same Height and Width. We create 2 circles: anchorOrigin and anchorEnd for respectively the origin and the end of the line. We add these circles to the canvas with ShapesCanvas.Children.Add(MyObject).

int size_EndLines = 25;
// Create 2 circles for the ends of the line
Ellipse anchorOrigin = new Ellipse
{
   Fill = new SolidColorBrush(Windows.UI.Colors.OrangeRed),
   Height = size_EndLines,
   Width = size_EndLines
};
ShapesCanvas.Children.Add(anchorOrigin);

Ellipse anchorEnd = new Ellipse
{
   Fill = new SolidColorBrush(Windows.UI.Colors.OrangeRed),
   Height = size_EndLines,
   Width = size_EndLines
};
ShapesCanvas.Children.Add(anchorEnd);

We set the position of each circle in order that its center matches the position of the end of the line. For this, we use two functions to modify the Canvas.Left and Canvas.Top attached properties of the circles:

// Put the anchors at the origin and at the end of the line
Canvas.SetLeft(anchorOrigin, line.X1 - size_EndLines / 2);
Canvas.SetLeft(anchorEnd, line.X2 - size_EndLines / 2);
Canvas.SetTop(anchorOrigin, line.Y1 - size_EndLines / 2);
Canvas.SetTop(anchorEnd, line.Y2 - size_EndLines / 2);

 

3. Add the event handlers for the circles

The strategy here is the exact same as described in the previous article with the Manipulation events applied to both anchors:

  • The ManipulationMode property can be used to restraint the move of the shape in only on direction. In our sample, we would like to move on both axis X and Y.
  • The ManipulationStarted event is raised when we start the drag & drop.
  • The ManipulationDelta event gives the possibility to give visual feedback for every move during the drag & drop.
  • The ManipulationCompleted event occurs when the drag & drop is finished.

Note: For the ManipulationDelta and ManipulationCompleted events, we are forced to use two different handlers because the code is modifying the position of the ends of the lines and we have to know which end (origin or end) to modify. The handler for the ManipulationStarted event is the same for both anchors.

// Enable manipulations on the anchors
anchorOrigin.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
anchorOrigin.ManipulationStarted += Anchor_ManipulationStarted;
anchorOrigin.ManipulationDelta += Anchor_Origin_ManipulationDelta;
anchorOrigin.ManipulationCompleted += Anchor_Origin_ManipulationCompleted;

anchorEnd.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
anchorEnd.ManipulationStarted += Anchor_ManipulationStarted;
anchorEnd.ManipulationDelta += Anchor_End_ManipulationDelta;
anchorEnd.ManipulationCompleted += Anchor_End_ManipulationCompleted;

 

4. Keep track of the selected line

We need to store some object to make the drag & drop working and to be also able to 'unselect' and 'select' a new line. Let me explain:

  • The line and its initial coordinates (x1,y1) - (x2,y2) are necessary to finalize the move when the drag & drop is complete.
  • The anchors of the line have to be deleted when the user click or touch another line.

To do this, we add a structure and we use it as a field that can be accessible by the code:

private struct ActiveLine
{
	public Line line;
	public double initialX1;
	public double initialY1;
	public double initialX2;
	public double initialY2;
	public Ellipse AnchorOrigin;
	public Ellipse AnchorEnd;
}

private ActiveLine activeLine;

Then, we create method to do the initialization:

private void InitializeActiveLine(Line line, Ellipse origin, Ellipse end)
{
   activeLine.line = line;
   activeLine.initialX1 = line.X1;
   activeLine.initialY1 = line.Y1;
   activeLine.initialX2 = line.X2;
   activeLine.initialY2 = line.Y2;
   activeLine.AnchorOrigin = origin;
   activeLine.AnchorEnd = end;
}

We use this method as the last instruction of the Line_Tapped event handler:

InitializeActiveLine(line, anchorOrigin, anchorEnd);

 

5. Unselect the line

This step is, in fact the very first we have to do while entering to the Line_Tapped event handler

private void Line_Tapped(object sender, TappedRoutedEventArgs e)
{
	// Remove the anchor from the selected line
	UnselectActiveLine();
	// ...

The code of UnselectActiveLine just put back the original color/stroke of the line and more important: delete the anchors for this line because we would like to have the anchors only for the lines we select.

public void UnselectActiveLine()
{
	if (activeLine.line != null 
		&& activeLine.AnchorOrigin!= null
		&& activeLine.AnchorEnd != null)
	{
		activeLine.line.Stroke = new SolidColorBrush(Windows.UI.Colors.Green);
		activeLine.line.StrokeThickness = 6;
		ShapesCanvas.Children.Remove(activeLine.AnchorOrigin);
		ShapesCanvas.Children.Remove(activeLine.AnchorEnd);
	}
}

Note: This 'unselection' is the way we chose for our code. The other possibility would be to use a 'switch to modification mode' button on the UI in order to go to a different mode: We would deactivate drawing on the InkCanvas and we could create/display all anchors of all lines to allow modifications.

 

6. All pieces of the Line_Tapped event handler

Let's put together all we just explained. Here is the final code of the Line_Tapped method:

private void Line_Tapped(object sender, TappedRoutedEventArgs e)
{
	// Remove the anchor from the selected line
	UnselectActiveLine();

	Line line = (Line)sender;
	line.Stroke = new SolidColorBrush(Windows.UI.Colors.DarkRed);
	line.StrokeThickness = 10;

	int size_EndLines = 25;
	// Create 2 circles for the ends of the line
	Ellipse anchorOrigin = new Ellipse
	{
		Fill = new SolidColorBrush(Windows.UI.Colors.OrangeRed),
		Height = size_EndLines,
		Width = size_EndLines
	};
	ShapesCanvas.Children.Add(anchorOrigin);

	Ellipse anchorEnd = new Ellipse
	{
		Fill = new SolidColorBrush(Windows.UI.Colors.OrangeRed),
		Height = size_EndLines,
		Width = size_EndLines
	};
	ShapesCanvas.Children.Add(anchorEnd);

	// Put the anchors at the origin and at the end of the line
	Canvas.SetLeft(anchorOrigin, line.X1 - size_EndLines / 2);
	Canvas.SetLeft(anchorEnd, line.X2 - size_EndLines / 2);
	Canvas.SetTop(anchorOrigin, line.Y1 - size_EndLines / 2);
	Canvas.SetTop(anchorEnd, line.Y2 - size_EndLines / 2);


	// Enable manipulations on the anchors
	anchorOrigin.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
	anchorOrigin.ManipulationStarted += Anchor_ManipulationStarted;
	anchorOrigin.ManipulationDelta += Anchor_Origin_ManipulationDelta;
	anchorOrigin.ManipulationCompleted += Anchor_Origin_ManipulationCompleted;

	anchorEnd.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
	anchorEnd.ManipulationStarted += Anchor_ManipulationStarted;
	anchorEnd.ManipulationDelta += Anchor_End_ManipulationDelta;
	anchorEnd.ManipulationCompleted += Anchor_End_ManipulationCompleted;

	InitializeActiveLine(line, anchorOrigin, anchorEnd);
}

 

The manipulations' events for the anchors

Initiate the manipulation

As described and explained in the previous article, we instantiate a TranslateTransform object (named dragTranslation) and we affect it to the RenderTransform property of the anchor. The goal is to apply a translation for every move during the drag & drop action.

// XAML Shapes manipulations
private TranslateTransform dragTranslation;
		
private void Anchor_ManipulationStarted(object sender,
	ManipulationStartedRoutedEventArgs e)
{
	Ellipse anchor = (Ellipse)sender;

	// Initialize the transforms that will be used to manipulate the shape
	dragTranslation = new TranslateTransform();
	anchor.RenderTransform = dragTranslation;
	anchor.Fill = new SolidColorBrush(Windows.UI.Colors.Orange);
}

 

Provide visual feedback

The handler for the ManipulationDelta has to know if we are handling/moving the origin or the end of the line in order to be able to move the line correctly. That is why we have two different handlers but calling the same sub method. The third parameter is a boolean indicating if we hare modifying the origin (true) or not (false):

private void Anchor_Origin_ManipulationDelta(object sender,
	ManipulationDeltaRoutedEventArgs e)
{
	AnchorManipulationDelta(sender, e, true);
}

private void Anchor_End_ManipulationDelta(object sender,
	ManipulationDeltaRoutedEventArgs e)
{
	AnchorManipulationDelta(sender, e, false);
}

So, in the AnchorManipulationDelta method,

  • We first modify the dragTranslation which acts on the anchor moves. This code effectively change the coordinates x and y of the anchor corresponding to the move of the mouse, pen or mouse.
  • After, we changes accordingly the position of either the origin or the end of the line.
private void AnchorManipulationDelta(object sender,
	ManipulationDeltaRoutedEventArgs e,
	bool OriginofLine)
{
	double x = e.Delta.Translation.X;
	double y = e.Delta.Translation.Y;
	dragTranslation.X += x;
	dragTranslation.Y += y;

	if (OriginofLine)
	{
		activeLine.line.X1 += x;
		activeLine.line.Y1 += y;
	}
	else
	{
		activeLine.line.X2 += x;
		activeLine.line.Y2 += y;
	}
}

 

Finish the manipulation

The same hack is used to identify if we did a move on the origin or on the end of the line.

private void Anchor_Origin_ManipulationCompleted(object sender,
	ManipulationCompletedRoutedEventArgs e)
{
	AnchorManipulationCompleted(sender, e, true);
}

private void Anchor_End_ManipulationCompleted(object sender,
	ManipulationCompletedRoutedEventArgs e)
{
	AnchorManipulationCompleted(sender, e, false);
}

The actions we have to perform for terminating the drag & drop are:

  • 'Resetting' the translation applied to the anchor
  • Getting the total translation on the X axis and Y axis with the parameter ManipulationCompletedRoutedEventArgs.Cumulative.Translation.
  • Adding this translation to the original coordinates of either the origin or the end of the line in order to modify definitely its position.
  • Affecting the new position of the end of the line to the field activeLine that tracks the selected line.
private void AnchorManipulationCompleted(object sender,
	ManipulationCompletedRoutedEventArgs e,
	bool OriginofLine)
{
	Ellipse anchor = (Ellipse)sender;
	anchor.RenderTransform = null;
	double x = e.Cumulative.Translation.X;
	double y = e.Cumulative.Translation.Y;
	Canvas.SetLeft(anchor, Canvas.GetLeft(anchor) + x);
	Canvas.SetTop(anchor, Canvas.GetTop(anchor) + y);

	anchor.Fill = new SolidColorBrush(Windows.UI.Colors.Black);

	if (OriginofLine)
	{
		activeLine.line.X1 = activeLine.initialX1 + x;
		activeLine.line.Y1 = activeLine.initialY1 + y;

		activeLine.initialX1 = activeLine.line.X1;
		activeLine.initialY1 = activeLine.line.Y1;
	}
	else
	{
		activeLine.line.X2 = activeLine.initialX2 + x;
		activeLine.line.Y2 = activeLine.initialY2 + y;

		activeLine.initialX2 = activeLine.line.X2;
		activeLine.initialY2 = activeLine.line.Y2;
	}
}

Here we are!

 

Moving the origin or the end of the line with an anchorMoving the origin or the end of the line with an anchor

 

 

Wrapping up

The Manipulation events are really powerful and can be applied to any XAML Shapes. In this sample, we just create two circles like anchors at the ends of the line in order to give the ability to the user to manipulate the line. We could think of building a more sophisticated UI with different buttons/shapes to apply translations, rotations or any actions on any other shapes of the application!

 

--

 

The source is available on GitHub - https://github.com/sbovo/UWP-Advanced-Inking

@sbovo for the AppConsult team.

 

 

Inking series' articles

This article is part of a series exploring concepts about inking and XAML Shapes. Here are all links:

  1. Use the UWP Inking platform as input for advanced scenarios
  2. Handling zoom in Inking applications
  3. Turning to the dark side of inking = UnprocessedInput
  4. Free your mind: Start manipulating XAML Shapes
  5. XAML Shapes manipulation level up ⇐ You are here

 

 

References