我需要在我的应用设计器中显示一些数据样本,这样我就可以开始设置我的页面外观,而无需运行它。我有以下代码
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>
但数据样本不会出现,而是在设计器中显示为字段名称。这是我的应用设计师的屏幕截图:http://i.stack.imgur.com/M1B8E.jpg
我一直在寻找这个问题的解决方案,但仍然没有运气,我只找到了WPF的解决方案,我担心如果UWP发生了什么变化。
任何帮助将不胜感激!
答案 0 :(得分:4)
尝试像这样绑定DataContext:
d:DataContext="{Binding Source={d:DesignInstance Type=local:SampleData, IsDesignTimeCreatable=True}}"
答案 1 :(得分:2)
简短形式也会起作用:
d:DataContext="{d:DesignInstance local:SampleData, IsDesignTimeCreatable=True}"
答案 2 :(得分:0)
完成示例,首先重建项目
xmlns:sampledata="using:UniversalProject.UWP.SampleData"
d:DataContext="{Binding Source={d:DesignInstance Type=sampledata:StepsSample, IsDesignTimeCreatable=True}}"