简体   繁体   中英

Design-time data sample in UWP

I need to display some data samples in my app designer so I can start styling my page appearance without having to run it. I have the following code

SampleData.cs

public class SampleData
{
    public ObservableCollection<Surat> ListSurat
    {
        get
        {
            return new ObservableCollection<Surat>()
            {
                new Surat() {Judul="Title 1", Nomor="1", Tanggal= new DateTime(2014,10,14), ID="414683" },
                new Surat() {Judul="Title 2", Nomor="2", Tanggal= new DateTime(2014,10,14), ID="414683" },
             };
        }
    }
}

App.xaml

<Application.Resources>
    <ResourceDictionary >
        <local:SampleData x:Key="SampleData"/>
    </ResourceDictionary>  
</Application.Resources>

MyPage.xaml

<ListView x:Name="ListView_DokumenMasuk"
                      ItemsSource="{Binding ListSurat}"
                      d:DataContext="{StaticResource SampleData}"
                      >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Width="450" Height="70">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid Grid.Row="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="2*"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Judul}" Grid.Column="0" FontSize="16"/>
                                    <TextBlock Text="{Binding Tanggal}" Grid.Column="1" FontSize="16"/>
                                </Grid>
                                <Grid Grid.Row="1">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="2*"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding Nomor}"/>
                                    <TextBlock Grid.Column="1">
                                        <Run Text="DocID : "/>
                                        <Run Text="{Binding ID}"/>
                                    </TextBlock>
                                </Grid>
                            </Grid>                                             
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>                    
            </ListView>

But the data samples won't appear, instead it appear in the designer as the field name. Here's the screenshot of my app designer : http://i.stack.imgur.com/M1B8E.jpg

I've been searching the solution for this problem but still no luck, I only found solution for WPF and I'm afraid if something has changed in the UWP.

Any help would be appreciated!

尝试绑定DataContext,如下所示:

d:DataContext="{Binding Source={d:DesignInstance Type=local:SampleData, IsDesignTimeCreatable=True}}"

简短形式也将起作用:

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

Complete sample, Rebuild project first

xmlns:sampledata="using:UniversalProject.UWP.SampleData" 
d:DataContext="{Binding Source={d:DesignInstance Type=sampledata:StepsSample, IsDesignTimeCreatable=True}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM