WPF 绑定实例之 LISTBOX显示 图片列表
2015-07-08 19:33
483 查看
WPF listbox 绑定实例 显示 图片列表
XAML:
[csharp] view
plaincopyprint?
<StackPanel>
<ListBox x:Name="lstImgs">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Width="100" Height="30" Source="{Binding Path=FullPath}">
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
CS:
[csharp] view
plaincopyprint?
public class Photo
{
public string FullPath { get; set; }
}
public MainWindow()
{
InitializeComponent();
this.InitPhoto();
}
public List<Photo> photos = new List<Photo>();
private void InitPhoto()
{
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
fbd.ShowDialog();
string rootPath = fbd.SelectedPath;
//MessageBox.Show(rootPath);
GetAllImagePath(rootPath);
lstImgs.ItemsSource = photos;
}
public void GetAllImagePath(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);
if (files != null && files.Length > 0)
{
foreach (var file in files)
{
if (file.Extension==(".jpg") ||
file.Extension == (".png") ||
file.Extension == (".bmp") ||
file.Extension == (".gif"))
{
photos.Add(new Photo()
{
FullPath = file.FullName
});
}
}
}
}
XAML:
[csharp] view
plaincopyprint?
<StackPanel>
<ListBox x:Name="lstImgs">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Width="100" Height="30" Source="{Binding Path=FullPath}">
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
CS:
[csharp] view
plaincopyprint?
public class Photo
{
public string FullPath { get; set; }
}
public MainWindow()
{
InitializeComponent();
this.InitPhoto();
}
public List<Photo> photos = new List<Photo>();
private void InitPhoto()
{
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
fbd.ShowDialog();
string rootPath = fbd.SelectedPath;
//MessageBox.Show(rootPath);
GetAllImagePath(rootPath);
lstImgs.ItemsSource = photos;
}
public void GetAllImagePath(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);
if (files != null && files.Length > 0)
{
foreach (var file in files)
{
if (file.Extension==(".jpg") ||
file.Extension == (".png") ||
file.Extension == (".bmp") ||
file.Extension == (".gif"))
{
photos.Add(new Photo()
{
FullPath = file.FullName
});
}
}
}
}
相关文章推荐
- [Err] 1449 - The user specified as a definer ('admin'@'%') does not exist
- 【ffmpeg】 音频转换命令
- HTML中调用servlet的问题(?)
- Redis的安装与使用
- Win7下无法安装VS2010旗舰版中VC9.0Runtime组件和.NET Framework 4.0组件
- Extjs前端细节处理
- Asp.net Vnext ModelBinding
- 使用cordova的加强webview
- SQL Server 2008 R2——创建函数 筛选出字符串中的数字 筛选出字符串中的非数字
- JavaScript HTML5 手机网站 基本功能
- C++ 之RTTI
- Android系统结构
- 【旅游】青岛自由行4天
- Android Support兼容包 详解
- 演示:win7如何向其他win7计算机发送消息
- 查看linux系统版本的命令
- iOS开发之实现半透明蒙层背景效果[用于下拉菜单页和分享页]
- Firefox下载自动保存
- Dapper 多数据库优化
- 单表ORM框架