WPF MVVM ViewModel constructor designmode
Posted
by Snake
on Stack Overflow
See other posts from Stack Overflow
or by Snake
Published on 2010-03-23T08:50:37Z
Indexed on
2010/03/23
8:53 UTC
Read the original article
Hit count: 523
Right,
I've got a main wpf window:
<Window x:Class="NorthwindInterface.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:NorthwindInterface.ViewModels" Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ViewModels:MainViewModel />
</Window.DataContext>
<ListView ItemsSource="{Binding Path=Customers}">
</ListView>
</Window>
And the MainViewModel is this:
class MainViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { };
public MainViewModel()
{
Console.WriteLine("test");
using (NorthwindEntities northwindEntities = new NorthwindEntities())
{
this.Customers = (from c in northwindEntities.Customers
select c).ToList();
}
}
public List<Customer> Customers { get;private set; }
Now the problem is that in designermode I can't see my MainViewModel, it highlights it saying that it can't create an instance of the MainViewModel. It is connecting to a database. That is why (when I comment the code the problem is solved).
But I don't want that. Any solutions on best practices around this?
© Stack Overflow or respective owner