在 WPF 中动态添加图像到 WrapPanel

Dynamically Added Image to WrapPanel in WPF

从 xml 中定义的图像列表向 WPF WrapPanel 添加了图像控件。 一切似乎就绪。我什至在调试中进行了检查,但没有任何东西是可见的。 有没有我遗漏的步骤?

        _printImages.ReadXml(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Images.xml"));

        if (_printImages.Tables.Contains("image") && _printImages.Tables["image"].Rows.Count > 0)
        {

            foreach (DataRow row in _printImages.Tables["image"].Rows)
            {
                // build info object
                ImageInfo imgInfo = new ImageInfo();

                imgInfo.Source = row["Source"].ToString();
                imgInfo.Custom = bool.Parse(row["Custom"].ToString());
                imgInfo.Font = row["Font"].ToString();
                imgInfo.FontSize = int.Parse(row["FontSize"].ToString());
                imgInfo.CharacterLimit = int.Parse(row["Characterlimit"].ToString());
                imgInfo.CustomType = row["Customtype"].ToString();

                _images.Add(imgInfo);

                //create control
                Image imgControl = new Image();
                BitmapImage imgFile = new BitmapImage();

                try
                {
                    imgFile.BeginInit();
                    imgFile.StreamSource = new FileStream(imgInfo.Source, FileMode.Open);
                    imgControl.Source = imgFile;
                    imgControl.Tag = _images.Count - 1;
                    imgControl.Height = Properties.Settings.Default.ImageHeight;
                    imgControl.Width = Properties.Settings.Default.ImageWidth;
                    imgControl.MouseDown += new MouseButtonEventHandler(image_MouseDown);
                    imgControl.Visibility = System.Windows.Visibility.Visible;
                    imageSelectionPanel.Children.Add(imgControl);
                }

                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Unable to create image");
                }


            }
        }

您的代码在设置 BitmapImage 的 StreamSource 属性 后缺少 EndInit 调用。

此外,加载位图后应关闭流,这通常由 using 块完成,并且还需要设置 BitmapCacheOption.OnLoad:

using (var stream = new FileStream(imgInfo.Source, FileMode.Open))
{
    imgFile.BeginInit();
    imgFile.StreamSource = stream;
    imgFile.CacheOption = BitmapCacheOption.OnLoad;
    imgFile.EndInit();
}

或者,也可以不使用 FileStream 直接从图像文件路径加载 BitmapImages:

var imgFile = new BitmapImage(new Uri(imgInfo.Source, UriKind.RelativeOrAbsolute));

您还可以创建一个包含 ImageInfo 对象集合的视图模型,并将 ItemsControl 绑定到该集合。 ItemsControl 将 WrapPanel 作为其 ItemsPanel,以及一个带有 Image 控件的 ItemTemplate:

<ItemsControl ItemsSource="{Binding ImageInfos}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding Source}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

有关详细信息,请参阅 MSDN 上的 Data Templating Overview 文章。