Skip to content

Design time data preview

eiadxp edited this page May 2, 2019 · 2 revisions

WPF came with default design time support to preview data binding objects through the d:DataContext="{d:DataInstance}", but that feature has some limitations:

  1. It will create only the defined object, and ignore all sub-objects in its properties (except for string objects). Let's say that you have a view model with a property named 'Models' that holds a list of your models, and you set it to your design time data context of you window, any control in your window (ListBox for example) will see the property 'Models' as null or empty list (unless you fill it in your view model constructor), and your list view will be empty.
  2. If you choose to create list of objects, WPF designer will create only 3 items for you, and you can not change this number.
  3. Xamarin Forms does not support data preview at all.

You can use our implementation of DataInstance in a very similar way to WPF to create data preview with the following features:

  1. You can control how deep the sub-objects will be created by setting the Depth property (default 2).
  2. You can set how many items in collection of objects will be created by setting the ItemsCount property (default 3).
  3. You can set what types will be created for your types and interfaces, for example you can set all properties of type ICollection<T> to be filled by ObservableCollection<T>, and just your property Models of type ICollection<Models> will be filled by List<Models>. This behavior can be controlled either by attributes or by configurations.

WPF:

You only need to set you design time context to our DesignInstance and specify your type:

d:DataContext="{tools:DesignInstance Type=vm:ViewModel}"

You can also set the properties: Depth, ItemsCount and IsCreateList to get more control on data preview.

Xamarin Form:

Here we do not have design time data context (yet!!), but you can set you binding context directly to our DesignInstance:

BindingContext="{tools:DesignInstance Type=vm:ViewModel}"

At design time the binding context will be set to a data preview version of ViewModel. You can control what will be set to your binding context by setting RuntimeBehavior property to:

  • OrignalValue: The default behavior, it will keep the old value of you binding context (usually null).
  • Null: It will set your binding context to null.
  • TypeCreate: It will create an instance of your type (view model) and set it to the binding context.

You can also set the properties: Depth, ItemsCount and IsCreateList to get more control on data preview.

Clone this wiki locally