【问题标题】:Error Dictionary Item 'DataTemplate' must have a Key attribute while set x:DataType="BitmapImage"错误字典项“DataTemplate”在设置 x:DataType="BitmapImage" 时必须具有 Key 属性
【发布时间】: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:”命名空间前缀。

标签: c# xaml uwp uwp-xaml


【解决方案1】:

您在ItemsControl.Resources 中定义DataTemplate。要将其用作您的项目的模板,您必须在 ItemsControl.ItemTemplate 中声明它。

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2011-03-08
    相关资源
    最近更新 更多