0

I'm struggling with DesignData in what should be a very simple case. I've defined the following trivial control in a Windows Universal Project

<UserControl
...
xmlns:local="clr-namespace:SimpleDataBind"
...
<UserControl.Resources>
    <DataTemplate x:Key="SimpleListTemplate">
        <TextBox Text="{Binding Name}"  />
    </DataTemplate>
</UserControl.Resources>
<Grid>
    <StackPanel Grid.Row="0" d:DataContext="{d:DesignData Source=./DesignData/Single.xaml}">
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
    <ListView Grid.Row="1" d:DataContext="{d:DesignData Source=./DesignData/Multiple.xaml}"
         ItemTemplate="{StaticResource SimpleListTemplate}"
         ItemsSource="{Binding collection}">
    </ListView>
</Grid>

The classes are defined as follows:

public class Element
{
    public Element() { }
    public string Name { get; set; }
}
public class ElementCollection : ObservableCollection<Element>
{
    public ElementCollection() { }
}

An element 'collection' of type ElementCollection is defined in the codeBehind to give a valid Binding Source.

Single.xaml is trivial:

 <local:Element xmlns:local="clr-namespace:SimpleDataBind" Name="Single" />

Multiple is equally so:

<local:ElementCollection xmlns:local="clr-namespace:SimpleDataBind" >
    <local:Element Name="Multiple Pete" />
    <local:Element Name="Multiple George" />
    <local:Element Name="Multiple John" />
</local:ElementCollection>

When viewed in the Designer, we should see the design data above. And indeed Control shows 'Single' as the Name correctly in the first row of the Grid. However the Control shows nothing in the second row of the Grid.

I'm clearly missing something obvious ...

jbhelicon
  • 93
  • 9

1 Answers1

0

OK, finally figured this. The solution is not to bind to the runtime object in the XAML; i.e., in my source code above, replace the element

    ItemsSource="{Binding collection}"

with the element

    ItemsSource="{Binding}"

Then you can obviously bind to the runtime object at ... runtime.

It's sort of interesting (and perhaps obvious to others more familiar with the tool) that it works this way, and the Microsoft documentation is a little obscure and out of date (the lads want to point you to Silverlight docs that don't work at all in Universal).

But anyway, hope this helps someone!

jbhelicon
  • 93
  • 9