/// <summary> /// Retrieves the visible containers in a LongListSelector and adds them to <paramref name="items"/>. /// </summary> /// <param name="list">LongListSelector that contains the items.</param> /// <param name="itemsPanel">Direct parent of the items.</param> /// <param name="items">List to populate with the containers currently in the viewport</param> /// <param name="selectContent"> /// Specifies whether to return the container or its content. /// For headers, we can't apply projections on the container directly (or everything will go blank), /// so we will apply them on the content instead. /// </param> private static void AddVisibileContainers(LongListSelector list, Canvas itemsPanel, List <KeyValuePair <double, FrameworkElement> > items, bool selectContent) { foreach (DependencyObject obj in VisualTreeExtensions.GetVisualChildren(itemsPanel)) { ContentPresenter container = obj as ContentPresenter; if (container != null && (!selectContent || (VisualTreeHelper.GetChildrenCount(container) == 1 && VisualTreeHelper.GetChild(container, 0) is FrameworkElement))) { GeneralTransform itemTransform = null; try { itemTransform = container.TransformToVisual(list); } catch (ArgumentException) { // Ignore failures when not in the visual tree break; } Rect boundingBox = new Rect(itemTransform.Transform(new Point()), itemTransform.Transform(new Point(container.ActualWidth, container.ActualHeight))); if (boundingBox.Bottom > 0 && boundingBox.Top < list.ActualHeight) { items.Add( new KeyValuePair <double, FrameworkElement>( boundingBox.Top, selectContent ? (FrameworkElement)VisualTreeHelper.GetChild(container, 0) : container)); } } } }
/// <summary> /// Helper function to see if any portion of the given frame is on screen. /// </summary> /// <param name="viewport">Where the viewport currently exists, in teh child's coordinates</param> /// <param name="content">The UI element of the thing we are trying to see if is in view.</param> /// <returns>True if the content is in the view port of the scroll bar.</returns> private bool isInFrame(Rect viewport, ContentPresenter content) { var transform = content.TransformToVisual(_itemContainer); var childBounds = transform.TransformBounds(new Rect(new Point(0, 0), content.RenderSize)); childBounds.Intersect(viewport); return(!childBounds.IsEmpty); }
/// <summary> /// Get the current visual bounds of the content. /// </summary> /// <returns>The current visual bounds of the content</returns> private Rect GetContentBounds() { Rect bounds = VisualTreeHelper.GetDescendantBounds(_content); GeneralTransform generalTransform = _content.TransformToVisual(this); Point topLeft = generalTransform.Transform(bounds.TopLeft); Point bottomRight = generalTransform.Transform(bounds.BottomRight); return(new Rect(topLeft, bottomRight)); }
private async Task RecognizeInkerText() { try { var inkRecognizer = new InkRecognizerContainer(); var recognitionResults = await inkRecognizer.RecognizeAsync(_inker.InkPresenter.StrokeContainer, InkRecognitionTarget.All); List <TextBox> boxes = new List <TextBox>(); var ttv = _contentPresenter.TransformToVisual(Window.Current.Content); Point offsetCoords = ttv.TransformPoint(new Point(0, 0)); foreach (var result in recognitionResults) { List <UIElement> elements = new List <UIElement>( VisualTreeHelper.FindElementsInHostCoordinates( new Rect( new Point( result.BoundingRect.X + offsetCoords.X, result.BoundingRect.Y + offsetCoords.Y), new Size(result.BoundingRect.Width, result.BoundingRect.Height)), _contentPresenter)); // find one with overlap var textBoxes = elements.Where(el => el is TextBox && (el as TextBox).IsEnabled); var maxOverlapArea = 0d; TextBox box = null; foreach (var textBox in textBoxes) { var bounds = textBox.TransformToVisual(_contentPresenter).TransformBounds(new Rect(0, 0, textBox.RenderSize.Width, textBox.RenderSize.Height)); var xOverlap = Math.Max(0, Math.Min(result.BoundingRect.Right, bounds.Right) - Math.Max(result.BoundingRect.Left, bounds.Left)); var yOverlap = Math.Max(0, Math.Min(result.BoundingRect.Bottom, bounds.Bottom) - Math.Max(result.BoundingRect.Top, bounds.Top)); var overlapArea = xOverlap * yOverlap; if (overlapArea > maxOverlapArea) { maxOverlapArea = overlapArea; box = textBox as TextBox; } } //TextBox box = elements.Where(el => el is TextBox && (el as TextBox).IsEnabled).FirstOrDefault() as TextBox; if (box != null) { var text = result.GetTextCandidates().FirstOrDefault().Trim(); if (!boxes.Contains(box)) { boxes.Add(box); box.Text = text; } else { box.Text += " " + text; } } } } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } } finally { _inker.InkPresenter.StrokeContainer.Clear(); } }