0

Following the suggested solution from this post, I still can't get the design time data to show up for my Windows Phone app using a ListBox of my custom type.

XAML

    <Page
        x:Class="MyApp.MainPage"
        ...
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d"
        DataContext="{Binding MyCommunicator.MyClass}"
        <UserControl.Resources>
            <myCommunicator:MyClassDesignTime x:Key="DesignViewModel"/>
        </UserControl.Resources>
        <ListBox x:Name="ItemsListBox" d:DataContext="{Binding Source={StaticResource DesignViewModel}}">
        ...
                <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="2*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Name"/>
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>

My custom type MyClassDesignTime is inherited from MyClass, has a parameterless constructor and static data that it returned from its properties (e.g. Name). I don't get any error messages when building or running my app, but during design time I cannot see any data, just a blank screen (in the "phone preview" window beside my XAML code.

Community
  • 1
  • 1
Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89

1 Answers1

1

Gorgsenegger,

In my personal practise, the easiest way of providing design time data in Windows Phone/Windows Store/WPF,... projects, is to use Blend to generate some sample data.

Open your project with Blend. Go to the Data Tab, right of Blend, Create Sample Data/New Sample Data.

Create datasource

Then design your design time collection with the same properties that you expect to find in your data items.

Rename properties

Then drag the collection from the data tab to your control on the design view or the object and timeline window. Then binding code will be made.

enter image description here

The binding will be made :

<ListBox DataContext="{Binding Source={StaticResource FurnitureDataSource}}"
  ItemsSource="{Binding Groups}"/>

Then you can observe your project. You will have some .xaml sample data added to your project holding the sample data that you can hand modify - or use the Data window to edit, change quantity.

You will also have a class added that represents your data and a .xsd file.

You can rework the generated code to use your class, but you'd need to arrange the xsd, file, ... I am not convinced it's the best practise. I prefer to keep two classes : mine and Blend one. So my class can get technically more complicated, and sample data will always work.
Time to time I 'll have to refactor in Blend when I change a property name.

Regards

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
  • @Gorgsenegger : Did you look the response ? I think I answered your question – Emmanuel DURIN Oct 08 '15 at 12:54
  • Thank you and sorry for the late response, I was held up by problems with Windows 10 on my system and didn't get round to trying out your suggested answer until now. Works perfectly now ;-) Thanks again! – Gorgsenegger Oct 22 '15 at 19:29