如何在文件夹中的StackPanel WPF中添加多个图像?
我想给folder path
并从该文件夹路径如果该folder contains 3 images
我想display those 3 images
到StackPanel WPF Form
我尝试过类似下面的内容,可以正常使用一个图像但是如何加载来自给定文件夹的所有图像?
private void Window_Loaded(object sender, RoutedEventArgs e) { Image i = new Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = new Uri("mypic.png", UriKind.Relative); // how to load all images from given folder? src.EndInit(); i.Source = src; i.Stretch = Stretch.Uniform; //int q = src.PixelHeight; // Image loads here sp.Children.Add(i); }
您应该使用如下所示的ItemsControl
。 它使用垂直StackPanel作为其项目的默认面板。
像这样设置ItemsControl的ItemsSource
:
imageItems.ItemsSource = Directory.EnumerateFiles(FOLDERPATH, "*.png");
从路径字符串到ImageSource
的转换是通过WPF中的内置类型转换执行的。
您可以使用不同的ItemsPanel,如下所示:
...