-
Notifications
You must be signed in to change notification settings - Fork 1
Design time data preview
WPF came with default design time support to preview data binding objects through the d:DataContext="{d:DataInstance}", but that feature has some limitations:
- 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.
- If you choose to create list of objects, WPF designer will create only 3 items for you, and you can not change this number.
- 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:
- You can control how deep the sub-objects will be created by setting the
Depthproperty (default 2). - You can set how many items in collection of objects will be created by setting the
ItemsCountproperty (default 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 byObservableCollection<T>, and just your propertyModelsof typeICollection<Models>will be filled byList<Models>. This behavior can be controlled either by attributes or by configurations.
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.

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.
