I am creating a simple WPF application to implement Datagrid database bindings using the Observable collection (after the MVVM template).
Class App.xaml.cs
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var mainWindow = new MainWindow(); var viewModel = new MainViewModel(); mainWindow.Show(); } }
when I try to associate it with my XAML, I get the following error:
Cannnot create an instance of "MainViewModel"
XAML Code:
<Window x:Class="MVVM_DemoAppl.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModel="clr-namespace:MVVM_DemoAppl.ViewModels" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <ViewModel:MainViewModel/> </Window.DataContext>
How to overcome this error? Thanks.
PS: I posted the same question on MSDN forums , but with all my code, kindly take a look at a better understanding.
As suggested by the user, should I support OnStartup () this way?
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); } }
source share