mtijn
mtijn

Reputation: 3678

how to add a textbox to a canvas

hi I have a C# app that features a canvas. I'd like to programmatically place a textbox (with text) on it. I've tried and tried but all I get is a fully transparent rectangle where my textbox ought to be. is it me or is this a known difficulty?

UPDATE: I should have mentioned.. (Sorry!) I'm also overriding OnRender in the object to be drawn like so:

    protected override void OnRender(DrawingContext drawingContext)
    {
        drawingContext.PushTransform(TransformRotation);
        Draw(drawingContext);
        drawingContext.Pop();
    }

and Draw is implemented like so:

    public override void Draw(DrawingContext drawingContext)
    {
        Rect graphicRectangle = Rectangle;
        ITransform2d transformToDisplay = Layer.TransformToDisplay;
        if (transformToDisplay != null)
        {
            graphicRectangle = new Rect(transformToDisplay.Transform(Rectangle.TopLeft),
                                        transformToDisplay.Transform(Rectangle.BottomRight));
        }
        textBox.Height = graphicRectangle.Height;
        textBox.Width = graphicRectangle.Width;
        Canvas.SetLeft(textBox, graphicRectangle.Left);
        Canvas.SetTop(textBox, graphicRectangle.Top);
    }

Upvotes: 2

Views: 3165

Answers (1)

brunnerh
brunnerh

Reputation: 184376

Canvas being a panel whose purpose is to arrange and display some kind of content i would recommend that you do not do anything like this.

If you need a Canvas with a TextBox use composition, for example create a UserControl with a TextBox over or under the Canvas and expose relevant properties and methods on the UserControl's interface.


Use Panel elements to position and arrange child objects in Windows Presentation Foundation (WPF) applications. - MSDN

Upvotes: 1

Related Questions