When for a designer I specify a VM-ку in DataContext which does not have a default constructor, he always curses it. How can I fix this situation?

 d:DataContext="{d:DesignInstance local:AuthenticationVm, IsDesignTimeCreatable=True}" 

enter image description here

  • And if you specify IsDesignTimeCreatable=False ? - VladD
  • @VladD, hmm, it helped. And what are the differences if true or false ? - Lightness

1 answer 1

If you specify IsDesignTimeCreatable=True , the WPF designer will try to create an object of this class and use it as a DataContext for display. For this, he needs a constructor without parameters.

If you have IsDesignTimeCreatable=False , the WPF designer will not attempt to create an object. Instead, it will create its fake data type with the same open properties, instantiate it, and use it as DataContext .


When you specify IsDesignTimeCreatable=False , you will no longer see the data in the designer. This may not be very convenient. Here is a more advanced way that allows you to see the data in the designer even if your VM does not have a constructor with no arguments.

For this you need to create data for the designer . Suppose we have such a VM class:

 class VM { public VM(string text) { Text = text; } public string Text { get; } } 

As we can see, this class has no constructor without parameters, and its properties do not have a setter. This is not a hindrance to us.

We get in the project ResourceDictionary (let it be called DesignDictionary.xaml , the exact name is not important). When you create it, it will look something like this:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyCoolProject"> </ResourceDictionary> 

Remove all of this (!) And put an instance declaration of your VM class instead:

 <local:VM xmlns:local="clr-namespace:MyCoolProject" Text="Design Text"/> 

Now go to the file properties and set it Build Action = DesignData and remove the Custom Tool :

design data

Now this instance can be used as follows:

 <Grid d:DataContext="{d:DesignData Source=DesignDictionary.xaml}"> <TextBlock Text="{Binding Text}"/> </Grid> 

Do not forget to recompile the project. Now the designer displays the data:

the result is obvious

Everything, we bypassed restrictions of the designer of WPF.

Literature: MSDN / WPF Designer blog / Sample Data in the WPF and Silverlight Designer .


Another idea to deal with the problem may be the creation of a special derived class (only for use in the designer), which will be the default constructor. This constructor will call the base constructor with some suitable parameters. In order not to accidentally use this class in real code, you can give it the [Obsolete] attribute.

  • Understood thanks! - Lightness
  • @Lightness: Look at the update response, added another interesting option. - VladD
  • Interesting approaches! Thank! - Lightness
  • @VladD, is it possible to make a constructor without parameters only for DesignInstance , so as not to accidentally use it in the code? - Gardes
  • one
    @ S.Kost: You can. Put it attribute [Obsolete] . - VladD