I have a program, which uses MS SQL database. I decided to switch to MVVM WPF pattern and use Entity Framework for database stuff and Catel for MVVM stuff.
I created database first model and long-term context in my code (Global.Database
). I changed ICollection and HashSet types in my model's *.tt file to ObservableCollection.
Now i have WPF View and View Model. At first i decided to show some basic database connection info on my view.
So, i created a model:
public class DbInfo :ModelBase
{
public DbInfo()
{
ServerName = Global.Database.Database.Connection.DataSource;
DbName = Global.Database.Database.Connection.Database;
RegisteredClientsCount = Global.Database.Clients.Count() - 1;
}
public string ServerName
{
get { return GetValue<string>(ServerNameProperty); }
set { SetValue(ServerNameProperty, value); }
}
public static readonly PropertyData ServerNameProperty = RegisterProperty("ServerName", typeof(string), string.Empty);
public string DbName
{
get { return GetValue<string>(DbNameProperty); }
set { SetValue(DbNameProperty, value); }
}
public static readonly PropertyData DbNameProperty = RegisterProperty("DbName", typeof(string), string.Empty);
public int RegisteredClientsCount
{
get { return GetValue<int>(RegisteredClientsCountProperty); }
set { SetValue(RegisteredClientsCountProperty, value); }
}
public static readonly PropertyData RegisteredClientsCountProperty = RegisterProperty("RegisteredClientsCount", typeof(int), 0);
}
It derives from ModelBase class, so, i guess, it has to be ObservableObject.
Then i created a viewmodel with just one property:
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
DatabaseInfo = new DbInfo();
}
public DbInfo DatabaseInfo
{
get { return GetValue<DbInfo>(DatabaseInfoProperty); }
set { SetValue(DatabaseInfoProperty, value); }
}
public static readonly PropertyData DatabaseInfoProperty = RegisterProperty("DatabaseInfo", typeof(DbInfo), null);
}
Then i created a label in my View and added a context binding:
Context="{Binding DatabaseInfo.RegisteredClientsCount}"
All i want is to find out, how the hell i can now track the changes in database? When i run my app, this label shows the proper value - the quantity of "clients" table rows. Then i add some rows manually into database - nothing happens in my window.
Maybe i have some missunderstanging with all of this stuff, but i expected my database data binded to wpf controls and wrapped into all these PropertyChanged interfaces and stuff being updated on database change. Am i wrong? What should I do? Thanks a lot for help.