1

Is there a way to add a control like in Win Forms C# Button b = new Button(); and then add it just like Form1.Controls.Add(b);

I can't do this in WPF because I don't have a Controls attribute in the Window class. How can I do this. I read this of just putting a Panel, dock it to fill and then there I use this:

myPanel.Children.Add(b);

But then it comes to me again. How do I create and add a Panel and dock it to fill?

If I use Panel p = new Panel(); it marks error. And how do I add the panel to my MainWindow?

2 Answers 2

4

You can add a stackpanel to window like below.

StackPanel myPanel = new StackPanel();
myWindow.Content = myPanel;

Like @HighCore said below, it is not cleaner to add controls in codebehind. Use XAML wherever it is possible and avoid adding controls in code behind

Sign up to request clarification or add additional context in comments.

15 Comments

-1 don't use procedural code to create or manipulate UI elements in WPF. That's what XAML is for.
+1 Using procedural code when OP explicitly asks for it.
@HighCore: If he manages to build an entire WPF application using only procedural code without noticing XAML, somehow I think the rest of the code won't be too shabby either. :)
Yes, I don't want to use XAML. I requested for C# Code. omg, thanks ~ It worked! Just changed some of the background stuff to the panel. It works nice now! Thanks! :3 @Sivasubramanian
@HighScore : My friend used to say there is one guy in SO who used to curse WinForms and praise WPF. I suspect you are the guy he is talking about. Dude you are famous in SO. :)
|
1

You can create any structure in C# as you can in XAML.

var p = new Panel();

Above doesn't work because Panel is an abstract class. You can, however do:

var p = new StackPanel();
var g = new Grid();
var wp = new WrapPanel();
var dp = new DockPanel();
//Etc.

See all panels here: http://msdn.microsoft.com/en-us/library/system.windows.controls.panel(v=vs.110).aspx

<Window ...>
    <StackPanel>
        <TextBox/>
    </StackPanel/>
</Window>

is equivalent with (from your MainWindow.xaml.cs):

var sp = new StackPanel();
sp.Children.Add(new TextBox);
Content = sp;

EDIT: Just to be clear, you ONLY want to create your UI in C# if it is dynamic in nature. If you know which fields will be there, always, use XAML. It is the HTML of WPF. Coding everything in the code-behind is the web-design equivalent of sending a blank HTML file and creating everything in JavaScript.

11 Comments

Ohh thanks, but I didn't know how to make the panel fill the form above so I can add the controls / elements over the form. I understood about creating the panel, just needed to know how to add it to my window to manipulate the form content. Anyways, thanks! :3
@user3576526 you don't "manipulate the form content" in WPF. Read about DataBinding. You're going about this in a totally wrong way. You will hit a really hard wall with your face...
@TroelsLarsen I disagree with your statement about dynamic UIs. If you're doing some kind of dynamic UI where you "add" and "remove" items at runtime, that's what an ItemsControl is for. Even in that case, a XAML+DataBinding approach is much preferable rather than a bunch of horrible winforms-like hacks.
That's right! "Coding everything in the code-behind is the web-design equivalent of sending a blank HTML file and creating everything in JavaScript." I always leae the Viual design in blank because I never use it. I love to create everything line by line x3
@HighCore: Well, doing Techonology X like Technology Y because you know Technology Y rarely leads to anything other than a huge refactoring 2-3 years down the line, when you realize the error of your ways :D I appreciate your intentions of trying to save OP at least some of that trouble.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.