wpf - C# "Controls" Does Not Exist in The Current Context -


error

in application i'm trying use "controls.add", visual studio keeps giving me error:

"error 1 name 'controls' not exist in current context c:\users\admin\desktop\demo\demo\mainwindow.xaml.cs 42 13 demo"

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes;  namespace demo {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();         }            private void button_click(object sender, routedeventargs e)         {             window1 window = new window1();             button button = new button();              window.windowstartuplocation = windowstartuplocation.centerscreen;             button.content = "new button";             button.visibility = visibility.visible;             button.height = 50;             button.width = 200;               controls.add(button); <-- error found here               window.show();           }     } } 

i'm using wpf, coming winforms. advice appreciated, thanks.

you need along lines of:

var window = new window1();  var stackpanel = new stackpanel { orientation = orientation.vertical };  button button = new button(); button.content = "new button"; button.visibility = visibility.visible; button.height = 50; button.width = 200;  stackpanel.children.add(button);  window.content = stackpanel; window.show(); 

although, i'd recommend defining ui components in xaml , reading on mvvm pattern.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -