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 TextBox
s 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
.