Warning that I'm relatively new to WPF, EF6 (code first) and MVVM, so I'm counting on this to be a rookie mistake. My views are linked via GalesoftMVVM, using a locator. The xaml file does indeed pick up on things defined in my viewmodel.cs file
I asked a question about this earlier yesterday and got recommended to implement INotifyPropertyChanged
, however even with that implemented my datagrid still only shows the headers and no rows unfortunately.
So far this is what I have, any tips or help would be appreciated.
What I would like to do is use the existing database to fill up the collection, and display it on the grid
Viewmodel.cs
public class View1 : ViewModelBase, INotifyPropertyChanged
{
private ObservableCollection<Table1> _columnCollection = new ObservableCollection<Table1>();
public ObservableCollection<Table1> ColumnCollection
{
get { return this._columnCollection; }
set { _columnCollection = value; }
}
}
View1.xaml
<DataGrid Grid.Row="1" Grid.RowSpan="6" x:Name="Datagrid" HeadersVisibility="Column" AutoGenerateColumns="False"
ItemsSource="{Binding Path=ColumnCollection, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
Datamodel class
public class Table1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _ID;
public int ID
{
get { return _ID; }
set
{
ID = value;
RaisePropertyChanged("ID");
}
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged("Name");
}
}
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
EFContext class
public class EFContext : DbContext
{
public EFContext() : base("name=EFConnectionstring")
{
}
public DbSet <Table1> SQLTestTable { get; set; }
}
There are 5 rows of test data in table 1. Headers do show up in the datagrid. (Even when auto-generated columns are turned off & the datatextcolumns are removed, so header lines are definitely within the collection, just not the rows)