[英]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 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 App.xaml中
<Application.Resources>
<ResourceDictionary >
<local:SampleData x:Key="SampleData"/>
</ResourceDictionary>
</Application.Resources>
MyPage.xaml 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 这是我的应用设计师的屏幕截图: 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. 我一直在寻找这个问题的解决方案,但仍然没有运气,我只找到了WPF的解决方案,我担心如果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}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.