如何从文件夹中添加StackPanel的WPF多张图片? [英] How do I add multiple images in StackPanel WPF from Folder?
问题描述
我想给文件夹路径
并从该文件夹路径如果文件夹包含3张图片
我要显示那些3图像
到的StackPanel WPF窗体
我试过?类似下面的正常工作为一个图像,但是从给定文件夹如何加载所有图片
<窗口x:类=wpfBug.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/ WinFX的/ 2006 / XAML
标题=窗口1HEIGHT =300WIDTH =300加载=Window_Loaded>
< StackPanel的名称=SP>
< / StackPanel的>
< /窗GT;
私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
图像I =新的图像();
BitmapImage的SRC =新的BitmapImage();
src.BeginInit();
src.UriSource =新的URI(mypic.png,UriKind.Relative);
//如何加载从给定的文件夹中的所有图像?
src.EndInit();
i.Source = SRC;
i.Stretch = Stretch.Uniform;
// INT Q = src.PixelHeight; //加载图像这里
sp.Children.Add(I)
}
您应该使用的ItemsControl
如下图所示。它采用了垂直的StackPanel作为其项目的默认面板
< ItemsControl的X:名称=imageItems>
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<图像来源={结合}保证金=10/>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>
将的ItemsSource
ItemsControl的像这样的
imageItems.ItemsSource = Directory.EnumerateFiles(FOLDERPATH,*。PNG);
从路径字符串到的ImageSource
的转换通过在WPF内置类型进行转换
您可以使用不同的ItemsPanel是这样的:
< ItemsControl的...>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
:
< / ItemsControl的>
I want to give folder path
and from that folder path If That folder contains 3 images
I want to display those 3 images
into StackPanel WPF Form
I tried something like below which works fine for one image but how can load all the images from given folder?
<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);
}
You should use an ItemsControl
like shown below. It uses a vertical StackPanel as default panel for its items.
<ItemsControl x:Name="imageItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Margin="5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Set the ItemsSource
of the ItemsControl like this:
imageItems.ItemsSource = Directory.EnumerateFiles(FOLDERPATH, "*.png");
The conversion from path string to ImageSource
is performed by built-in type conversion in WPF.
You may use a different ItemsPanel like this:
<ItemsControl ...>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
...
</ItemsControl>
这篇关于如何从文件夹中添加StackPanel的WPF多张图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!