Friday, 15 May 2015

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


error

In my application, I am trying to use "Controls.Add" But the Visual Studio is giving me the error:

"Error 1 name 'control' is not present in current context C: \ Users \ Admin \ Desktop \ Demo \ Demo \ MainWindow.xaml.cs 42 13 Demo "

 Using the  system; Using System.Collections.Generic; Using System.Linq; Using System.Text; System usage threading. Task; Using System.Windows; Using System.Windows.Controls; Using System.Windows.Data; Using System.Windows.Documents; Using System.Windows.Input; System.Windows.Media; System.Windows.Media Using imaging; Using System.Windows.Navigation; Using System.Windows.Shapes; Name Locations Demo {/// & lt; Summary & gt; /// MainWindow.xaml for Interaction Logic /// & lt; / Summary & gt; Public Partial Class Main Window: Window {Public Main Window () {Initial Group (); } Private Zero Button_Click (Object Sender, RoutedEventArgs E) {Window 1 Window = New Window 1 (); Button button = new button (); Window.WindowStartupLocation = WindowStartSpace Center screen; button. Content = "new button"; button. Visibility = visibility Visible; button. Heights = 50; button. With = 200; Controls.Add (button); & Lt; - The error came here. Show (); }}}  

I am using WPF, coming from WinForms. Any advice is appreciated, thanks.

You have to do something on the lines of this:

  var window = New window1 (); Var stackPanel = New stackpanyel {Orientation = Orientation. Vertical}; Button button = new button (); button. Content = "new button"; button. Visibility = visibility Visible; button. Heights = 50; button. With = 200; StackPanel.Children.Add (button); Window.Content = stackPanel; Window.Show ();  

However, I recommend defining all UI components in XAM and reading on MVVM pattern.


No comments:

Post a Comment