【发布时间】:2017-01-05 02:21:05
【问题描述】:
我正在尝试实现示例通用 Windows 平台应用程序以显示网格中的图像数量,当我将图像源框架到项目面板中然后构建解决方案得到此错误“错误字典项目'数据模板'必须有一个关键属性" 请任何人提出一些建议或帮助我。
public ObservableCollection<BitmapImage> ImgList = new ObservableCollection<BitmapImage>();
for (int i = 1; i < 15; i++)
{
var image = new Image
{
Source = new BitmapImage(
new Uri(
"https://canaryappstorage.blob.core.windows.net/dummy-container/food"+i+"_tn.jpg"))
};
var bitmapImage = new BitmapImage();
ImgList.Add(image.Source as BitmapImage);
image.Source = null;
}
<ItemsControl ItemsSource="{Binding ImgList2}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate x:DataType="BitmapImage">
<StackPanel Orientation="Horizontal">
<Image Width="200"
Height="100"
Source="{x:Bind }"
Stretch="UniformToFill" />
</StackPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
【问题讨论】:
-
从 DataTemplate 的 DataType 属性中删除“x:”命名空间前缀。