Question

I'm having a ViewModel that has an InternalViewModel-type property, which in turn has a Collection property.

Given that a view's DataContext is an instance of ViewModel, binding the internal collection would require the following syntax:

{Binding InternalViewModelProperty.Collection}

At runtime it works just as expected. However, if I use a DesignInstance as a proxy to represent my ViewModel object, at design time it won't see the collection at all (no columns autogenerating, for example).

If I cheat and expose the Collection property as a ViewModel property and changes the binding to:

{Binding Collection}

Then it works again at design time.

Is there a reason why a collection nested as a property within another property belonging to the ViewModel behaves differently at design time? Is this a limitation of DesignInstance?

Here's the XAML code:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:LocalNS"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    mc:Ignorable="d"
    x:Class="MyUserControl"
    d:DataContext="{d:DesignInstance Type=local:MyViewModel}"
    >
<Grid>
    <DataGrid ItemsSource="{Binding InternalViewModelProperty.Collection}"/>
</Grid>
</UserControl>
Was it helpful?

Solution

First of all, the problem may be simply that you need to add IsDesignTimeCreatable=True to your d:DesignInstance declaration, like this:

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

If that doesn't help, the issue might be that your view model does not have a parameterless constructor or the it fails to set a value for InternalViewModelProperty in its constructor.

In other words, your view model needs to have a constructor with no parameters, and within that constructor, you need to set InternalViewModelProperty to a non-null value. Further, you need to make sure the Collection property of InternalViewModelProperty is also set to a non-null value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top