C# 错误字典项';数据模板';设置x:DataType=”时必须具有键属性;“位图图像”;

C# 错误字典项';数据模板';设置x:DataType=”时必须具有键属性;“位图图像”;,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,我正在尝试实现一个示例通用Windows平台应用程序,用于显示网格中的图像数量,当我将图像源框到项目面板中,然后生成解决方案时,会出现以下错误“错误字典项目‘数据模板’必须有一个键属性” 请任何人提出一些建议或帮助我 public ObservableCollection<BitmapImage> ImgList = new ObservableCollection<BitmapImage>(); for (int i = 1; i < 15; i++) {

我正在尝试实现一个示例通用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>
公共ObservableCollection ImgList=新ObservableCollection(); 对于(int i=1;i<15;i++) { var图像=新图像 { Source=新位图图像( 新Uri( "https://canaryappstorage.blob.core.windows.net/dummy-container/food“+i+”_tn.jpg”)) }; var bitmapImage=新的bitmapImage(); ImgList.Add(image.Source作为位图图像); image.Source=null; }
您正在
ItemsControl.Resources
中定义
数据模板。要使其作为项目的模板工作,必须在
ItemsControl.itemstemplate

中声明它,从DataTemplate的DataType属性中删除“x:”命名空间前缀。