-1

I am attempting to self-teach C# and have read a sizable number of textbooks and training programs which have been useful. I'm now attempting to build my first application using C# and WPF/XAML. The application is intended to be a simple "Employee Records" app that stores some basic information from the UI into a list. However, I don't seem to be able to map the TextBoxs in the UI to the code.

I have my Label and TextBox defined in MainWindow.xaml like this:

<Label x:Name="lbl_forename" Content="Forename" HorizontalAlignment="Left" Margin="20,35,0,0" VerticalAlignment="Top" Width="220" Height="26" Grid.ColumnSpan="2"/>
<TextBox x:Name="txt_forename" HorizontalAlignment="Left" Height="23" Margin="20,61,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="220" Grid.ColumnSpan="2"
                    Text="{Binding Path=Forename, Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged, 
                    ValidatesOnDataErrors=True,
                    ValidatesOnExceptions=True}" 
                 Style="{StaticResource ResourceKey=errorAwareTextBox}"/>

I have a class file People.cs:

public class People
{
   private string firstname; //field
   public string FirstName //property
   {
      get { return firstname; }
      set { firstname = value; }
   }
}

And I have tried building my list within the same class like this:

public static List<People> BuildList()
{
   //Store the list or collection of the data here
   return new List<People>
   {
      { new People() { firstname = Forename } }
   };
}

However, I'm getting:

Error CS0103 The name 'Forename' does not exist in the current context

The same applies where I use txt_Forename and not Forename.

The same applies where I have the list in public partial class MainWindow : Window.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
Dan Wyatt
  • 19
  • 1
  • Your answer is probably available in this post: https://stackoverflow.com/questions/1725554/wpf-simple-textbox-data-binding – Sh.Imran Jul 27 '20 at 11:06
  • 1
    if you want the text from the `TextBox` to be the first name, then use this: `{ new People() { firstname = MainWidow.txt_forename.Text} }`. That also implies that the people class knows your view class or where ever that BuildList method is – Nawed Nabi Zada Jul 27 '20 at 11:45
  • 1
    You question is very confusing and unclear. Basically what you do is not correct. The error is coming from the firstname = Forename part. Forename is not known in that context. Perhaps if you could explain why you expected it to be there, maybe someone could help – Nawed Nabi Zada Jul 27 '20 at 11:56

1 Answers1

1

Short answer:

The error comes from the fact that you are trying to access instance members from a static method.


Longer answer:

In WPF, when you give an XAML element a Name, it gets declared as an instance field of the class being created from that XAML file. A "field" being a named variable associated with a class, and "instance" meaning that each instance of that class has its own, separate variable by that name.

So, when you put <TextBox x:Name="txt_forename"... into your MainWindow.xaml. Your MainWinow class gets a field named txt_forename. Just as if you had added the code TextBox txt_forename; into the MainWindow.xaml.cs file.

When the WPF application loads, it creates an instance of the "startup" window (e.g. MainWindow) and shows that instance of the window. But just like your Person class, there can potentially be more than one MainWindow instance and each one would have its own txt_forename.

Now let's take your method public static List<People> BuildList(). I'm going to assume for the moment that it's declared inside the MainWindow class.

The problem with this is that it's static. A static member of a class is shared by all instances of that class and so can't access fields/properties of any specific instance without being told which one. If you want to call BuildList() from inside an instance of MainWindow and have it look at the current value of Forename, you don't want a static method.

If BuildList() is declared outside of MainWindow (for some reason), or if you want it to remain static (again, for some reason), then you'd need to add a parameter to your method to tell it which instance of MainWindow you want to use. Something like:

public static List<People> BuildList(MainWindow window)
Keith Stein
  • 6,235
  • 4
  • 17
  • 36