Reputation: 13
I am trying to get started learning WPF. I added a reference of PresentationCore, PresentationFramework, System.XAML, and WindowsBase to my Class Library and entered the following code:
internal struct Program
{
static void Main(string[] args)
{
new MainScreen().Open();
}
}
internal struct MainScreen
{
private Window _Window;
private Grid _LayoutRoot;
internal void Open()
{
_Window = new Window();
_LayoutRoot = new Grid();
_Window.Content = _LayoutRoot;
addBackground();
addDataGrid();
_Window.WindowState = WindowState.Maximized;
_Window.ShowDialog();
}
private void addBackground()
{
LinearGradientBrush bkg = new LinearGradientBrush();
GradientStopCollection grdCol = new GradientStopCollection();
GradientStopCollection grdStops = new GradientStopCollection();
grdStops.Add(new GradientStop(Color.FromArgb(255, 150, 150, 150), 0));
grdStops.Add(new GradientStop(Color.FromArgb(255, 235, 235, 235), .7));
grdStops.Add(new GradientStop(Color.FromArgb(255, 150, 150, 150), 1));
bkg.GradientStops = grdStops;
_LayoutRoot.Background = bkg;
}
private void addDataGrid()
{
DataGrid dgrRecords = new DataGrid();
dgrRecords.Margin = new Thickness(0, 70, 0, 0);
dgrRecords.IsReadOnly = true;
DataGridColumn colID = new DataGridTextColumn();
DataGridHelper grdHelper = new DataGridHelper(dgrRecords, new GetSelected(processGridSelection));
grdHelper.SetDoubleClick();
grdHelper.AddColumn("colID", "ID", "ID");
grdHelper.AddColumn("colLastName", "Last Name", "LastName");
grdHelper.AddColumn("colFirstName", "First Name", "FirstName");
List<Person> People = new List<Person>();
People.Add(new Person(0, "FName1", "LName1"));
People.Add(new Person(1, "FName100", "LName100"));
dgrRecords.ItemsSource = People;
_LayoutRoot.Children.Add(dgrRecords);
}
private void processGridSelection(object item)
{
Person person = (Person)item;
MessageBox.Show(person.LastName);
}
}
internal struct DataGridHelper
{
private DataGrid _DataGrid;
private GetSelected _GetSelected;
internal DataGridHelper(DataGrid dgr, GetSelected getSelected)
{
_DataGrid = dgr;
_GetSelected = getSelected;
_DataGrid.AutoGenerateColumns = false;
}
internal void SetDoubleClick()
{
_DataGrid.MouseDoubleClick += new MouseButtonEventHandler(mouseDoubleClick);
}
internal void AddColumn(string colName, string header, string fieldBinding)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Binding = new Binding(fieldBinding);
col.Header = header;
_DataGrid.Columns.Add(col);
}
private void mouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (_DataGrid.SelectedIndex < 0)
return;
_GetSelected.Invoke(_DataGrid.SelectedItem);
}
}
internal enum Screen
{
Person,
People
}
internal delegate void GetSelected(object item);
I get the following error when I try to open the application:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll
Additional information: The calling thread must be STA, because many UI components require this.
I am stuck on how to fix this, especially since I'm not using any multi-threading. Also, I don't know if this matters, except it seems all the components, such as Window, Grid, DataGrid, etc. are all classes instead of structures. I already had to modify my DatagridHelper structure to avoid a construction error that I could have avoided if I had used a class. Is WPF kind of biased towards object-oriented programming?
Upvotes: 0
Views: 64
Reputation: 7601
You have to Add [STAThread]
at main Method like
[STAThread]
static void Main(string[] args)
{
new MainScreen().Open();
}
Upvotes: 2