I cant seem to figure out how to get data from wfp into a list and bind to datagrid.
The xaml form is displaying alright
I have created properties, I created a class to inherit the base class which is code360. I have tried a lot of resources online but not working for me. I tried to hard code the input to see if it will populate the grid but no way. Like this
{firstName = "Tim", lastName = "Joy",
email = "tim@joy.com",
phoneNumber = "0988390243",
amount = 200000 }
This is mainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
namespace Code360
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class code360
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string phoneNumber { get; set; }
public decimal amount { get; set; }
}
public class Code360Manager : List<code360>
{
public Code360Manager()
{
Add(new code360() {firstName = "Tim", lastName = "Joy", email = "tim@joy.com", phoneNumber = "0988390243", amount = 200000 });
}
}
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
//studentGrid.ItemsSource = Code360Manager();
MessageBox.Show("This is to test the firstname", firstName.Text);
}
}
}
This is mainWindow.xaml
<Window x:Class="Code360.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Code360"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="1" FontSize="36"
Text="Code360 Student Record" Grid.ColumnSpan="3"
Margin="0,0,0,10" />
<TextBlock Grid.Column="1" Grid.Row="2"
Text="First Name" Margin="0,0,0,10"/>
<TextBox x:Name="firstName" Grid.Column="2" Grid.Row="2"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="3" Grid.Row="2" Text="Last Name" Margin="0,0,0,10"/>
<TextBox x:Name="lastName" Grid.Column="4" Grid.Row="2" Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="3"
Text="Email" Margin="0,0,0,10"/>
<TextBox x:Name="email" Grid.Column="2" Grid.Row="3"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="3" Grid.Row="3"
Text="Phone Number" Margin="0,0,0,10"/>
<TextBox x:Name="phonenumber" Grid.Column="4" Grid.Row="3"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="4"
Text="Amount" Margin="0,0,0,10"/>
<TextBox x:Name="amount" Grid.Column="2" Grid.Row="4"
Width="150" Margin="0,0,0,10"/>
<Button x:Name="submitButton" Content="Submit"
Grid.Column="4" Grid.Row="4"
Margin="0,0,0,10" Click="SubmitButton_Click" />
<DataGrid x:Name="studentGrid" Grid.Column="1" Grid.Row="5" MinWidth="20" Grid.ColumnSpan="5" Margin="22,0,45,154" Grid.RowSpan="2" ItemsSource="{StaticResource Code360Manager}">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding firstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding lastName}" />
<DataGridTextColumn Header="Email"
Binding="{Binding email}" />
<DataGridTextColumn Header="Phone Number" Binding="{Binding phonenumber}" />
<DataGridTextColumn Header="Amount" Binding="{Binding amount}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Grid>
</Window>
I believe that you need to set DataContext. After InitializeComponent();: DataContext = this; Edit: (Sorry. I sent the comments badly). You need to set the xaml to the context. Generally using a MainWindowViewModel. I change your code to set the ItemsSource to a List<> instead a class:
public partial class MainWindow : Window
{
public List<code360> Code360Manager { get; set; } = new List<code360>();
public MainWindow()
{
InitializeComponent();
Code360Manager.Add(new code360() {firstName = "Tim", lastName = "Joy", email = "tim@joy.com", phoneNumber = "0988390243", amount = 200000 });
DataContext = this;
}
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
//studentGrid.ItemsSource = Code360Manager();
MessageBox.Show("This is to test the firstname", firstName.Text);
}
}
public class code360
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string phoneNumber { get; set; }
public decimal amount { get; set; }
}
1.To use Binding
u need to set the DataContext
property so
InitializeComponent();
this.DataContext = this;
2.u need a collection to bind the DataGrid
in xaml.cs List<code360> MyList { get; set; }
List<code360> MyList { get; set; }
List<code360> MyList { get; set; }
// make sure u have data in this list
3. ItemsSource
prop will take collection so u need to do bind it with collection ItemsSource="{Binding MyList }
// note the list name is same as the in 2 ponit
also this is not following mvvm
I pasted that code in my Visual Studio and among other things, I came across a problem with this line ItemsSource="{StaticResource Code360Manager}"
From the XAML you shared I cannot see where you defiend the resource so consider something like the following.
<Grid.Resources>
<local:Code360Manager x:Key="Code360Manager" />
</Grid.Resources>
after the definition of grid and you'll see the datagrid render
A Grid will not take data input as ItemSource, you need to use DataContext. Something like:
studentGrid.DataContext = Code360Manager();
If it still does not work, try using ListCollectionView as following:
ListCollectionView alist = new ListCollectionView(Code360Manager());
studentGrid.DataContext = alist;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.