Search code examples
c#xamluwpuwp-xamlwin2d

My Win2D CanvasControl doesn't automatically resize itself based on its content's dimensions. (UWP)


So I am creating a text editor via Win2D to get past the UWP playtform's RichEditBox limitations. Basically I have a CanvasControl that I use to draw text to via the Draw event's args.DrawingSession.DrawTextLayout() method.

The issue is that CanvasControl does not resize itself to accommodate content that is being drawn within it. Is there a feature that I am failing to enable, or should this be done manually during each call to the Draw event handler? What is the most efficient approach to handling this? Thanks!


Solution

  • CanvasTextLayout.LayoutBounds.Height is a description of the rendering height inside the CanvasControl, but this is not the height of the CanvasControl control itself (in fact, CanvasControl.Height always shows NaN in Debug).

    Only when the Height property of the CanvasControl is greater than the height of the ScrollViewer, the scroll bar will be displayed.

    So after you render the text, you can manually set the Height of the CanvasControl.

    // render code
    // ...
    double height = textLayout.LayoutBounds.Height;
    canvasControl.Height = height;
    

    Best regards.