Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文件夹在StackPanel WPF中添加多个图像?_C#_Wpf_Wpf Controls_Wpfdatagrid - Fatal编程技术网

C# 如何从文件夹在StackPanel WPF中添加多个图像?

C# 如何从文件夹在StackPanel WPF中添加多个图像?,c#,wpf,wpf-controls,wpfdatagrid,C#,Wpf,Wpf Controls,Wpfdatagrid,我想给出文件夹路径,如果文件夹包含3个图像,我想将这3个图像显示到StackPanel WPF表单中 我尝试了下面这样的方法,对于一个图像来说效果很好,但是如何从给定的文件夹加载所有的图像呢 <Window x:Class="wpfBug.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/

我想给出
文件夹路径
,如果
文件夹包含3个图像,我想
将这3个图像显示到
StackPanel WPF表单中

我尝试了下面这样的方法,对于一个图像来说效果很好,但是如何从给定的文件夹加载所有的图像呢

<Window x:Class="wpfBug.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <StackPanel Name="sp">
    </StackPanel>
</Window>



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);
        }

已加载私有无效窗口(对象发送器、路由目标)
{
图像i=新图像();
BitmapImage src=新的BitmapImage();
src.BeginInit();
src.UriSource=新Uri(“mypic.png”,UriKind.Relative);
//如何从给定文件夹加载所有图像?
src.EndInit();
i、 来源=src;
i、 拉伸=拉伸。均匀;
//int q=src.PixelHeight;//此处加载图像
sp.Children.添加(i);
}

您应该使用如下所示的
项目控件。它使用垂直堆叠面板作为其项目的默认面板

<ItemsControl x:Name="imageItems">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Margin="5"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
从路径字符串到
ImageSource
的转换是通过WPF中的内置类型转换执行的


您可以使用不同的ItemsPanel,如下所示:

imageItems.ItemsSource = Directory.EnumerateFiles(FOLDERPATH, "*.png");
<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    ...
</ItemsControl>

...

我认为所有的图像都互相重叠:(如何在两个图像之间留出空间?设置它们的边距属性。仍然只显示一个图像:奇怪,我写了
imageItems.ItemsSource=Directory.EnumerateFiles(FOLDERPATH,*.png”)
窗口加载时
不确定您的意思。如果您设置了Items控件的大小,则应确保图像可以适合其大小。您可以为图像控件指定适当的(较小的)大小大小。或将ItemsControl放入ScrollViewer。将ItemsSource替换为ListBox。默认情况下,它有一个垂直滚动条。如果它也只显示一个图像,则文件夹中只有一个(png)。