views:

49

answers:

2

I have a WPF application which I am using to learn MVVM and IoC.

The problem is that the Model used by one of the Views expects to pull one of its dependancies in the constructor from an IoC container.

When working on this View in the Visual Studio designer it cannot show the design because an exception is being raised in the model.

Is there a way around this? Am I pulling my dependancies in the wrong place in code or is there a way I can pass in constructed dependancies, perhaps through Constructor injection.

At present the IoC container is setup in code in App.xaml.cs.

The IoC container is a roll-your-own taken from this article on MSDN - http://msdn.microsoft.com/en-us/magazine/cc337885.aspx

A: 

I was facing a similar problem recently. I worked around it with the following check:

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            Init();
    }
Jimmie R. Houts
Thanks - similar to how it was done in WinForms.
benPearce
A: 

I found the problem was caused by declaring the ViewModel as the View's datacontext within the Xaml, by moving this to the View constructor the Xaml renders properly in the designer.

benPearce