I'm trying to create dynamically text box in WPF. It is very essential that I will have the flexibility to determine where the text box will be - in pixel level.
I have found many answers which use stackpanel to create "run-time" text box - but couldn't find how to construct it according to specified location.

the textbox has to be "word wrap" and I'm using a button click event to create the text box

this is the code for now, I really don't know which methods or properties will be helpful.
thanks :)

private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextBox x = new TextBox();
            x.Name = "little";
            x.TextWrapping= TextWrapping.Wrap;
            x.VerticalScrollBarVisibility=ScrollBarVisibility.Visible;
            x.AcceptsReturn = true;
            x.Margin = new Thickness(5, 10, 0, 0);

        }

You generally use the Children property of any class that derives from Panel. For example:

stackPanel.Children.Add(x);

WPF handles layout differently than WinForms. In WinForms, each control can handle its own position (although a LayoutEngine can override this). In WPF, the parent control positions its children. A StackPanel will position controls one after the other, either horizontally or vertically. You may want to look at the Canvas class. It positions controls relative to its local origin (top left corner of the panel). You would then use the static methods (which are wrappers for attached dependency properties) to sets the controls position. For example:

canvas.Children.Add(x);
canvas.SetLeft(x, 16.0);
canvas.SetTop(x, 32.0);

Sizing is handled through the Width and Height propties of the TextBox. Also note that WPF does not use pixels (note that I used double values in the above example), it uses logical units (which are essentially pixels when using 96 DPI). This can create some issues, since the WPF rendering engine will use anti-aliasing, which can lead to "fuzzy" lines. This may not be relevant in this case, but keep it in the back of your mind, and look into the SnapToDevicePixels (all versions of .NET) and UseLayoutRounding (.NET 4.0 only) properties if it becomes an issue. Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.