问题描述
我是WPF的新手。我想从驱动器中显示不同的图像,并在网格中显示2行3列并进行分页。
我可以使用下面的单列显示图像代码
I am new to WPF.I want to show different images from drive and show it in grid with 2 rows and 3 columns with paging.
I am able to display images in single column using below code
XMAL :
<DatGrid>
<DataGrid.Columns >
<DataGridTemplateColumn Header="SampleImages">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="70" Width="70" Source="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns></DatGrid>
XMAL.CS
string path = Environment.CurrentDirectory +\\snapshot\\;
List< string> imageList = new List< string>();
string [] images = System.IO.Directory.GetFiles(path);
Image image = new Image();
foreach(图片中的字符串Img)
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(Img,UriKind.Relative);
bmp.EndInit();
image.Source = bmp;
//imageList.Add(new Image {Source = bmp});
imageList.Add(Img);
}
imageDataGrid.ItemsSource = imageList;
请注意这些图像是在运行时生成的,并存储在代码中指定的路径上。
但不能以分页显示2行3列。我也想使用MVVM。
搜索了很多但却无法取得任何丰硕成果。任何人都可以指导我。
在此先感谢。
XMAL.CS
string path = Environment.CurrentDirectory+ "\\snapshot\\";
List<string> imageList = new List<string>();
string[] images = System.IO.Directory.GetFiles(path);
Image image= new Image();
foreach (string Img in images)
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(Img, UriKind.Relative);
bmp.EndInit();
image.Source = bmp;
//imageList.Add(new Image { Source = bmp });
imageList.Add(Img);
}
imageDataGrid.ItemsSource = imageList;
Please note these images are generated run time and stored on specified path in code.
But not able to display in 2 rows and 3 columns with pagination.I also want to use MVVM.
Searched alot but not able to get any fruitful results.Can anybody guide me in the same.
Thanks in advance.
推荐答案
<grid>
<rowdefinitions>
<rowdefinition height="Auto" />
<rowdefinition height="Auto" />
</rowdefinitions>
<columndefinitions>
<columndefinition width="Auto" />
<columndefinition width="Auto" /><code></code>
<columndefinition width="Auto" />
</columndefinitions>
<image source="{Binding}" grid.row="0" grid.column="1" />
<image source="{Binding}" grid.row="0" grid.column="2" />
<image source="{Binding}" grid.row="0" grid.column="3" />
<image source="{Binding}" grid.row="1" grid.column="1" />
<image source="{Binding}" grid.row="1" grid.column="2" />
<image source="{Binding}" grid.row="1" grid.column="3" />
</grid>
这篇关于在2p 3列网格中显示不同的图像,并在wpf中显示分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!