WPF 绑定实例之 LISTBOX显示 图片列表

WPF  listbox  绑定实例 显示 图片列表

XAML:



[csharp]  view plain copy print ?
  1. <StackPanel>  
  2.        <ListBox x:Name="lstImgs">  
  3.            <ListBox.ItemTemplate>  
  4.                <DataTemplate>  
  5.                    <Image Width="100" Height="30" Source="{Binding Path=FullPath}">  
  6.                          
  7.                    </Image>  
  8.                </DataTemplate>  
  9.            </ListBox.ItemTemplate>  
  10.        </ListBox>  
  11.    </StackPanel>  




CS:


   
[csharp]  view plain copy print ?
  1. public class Photo  
  2.    {  
  3.        public string FullPath { getset; }  
  4.    }  
  5.   
  6.   
  7.        public MainWindow()  
  8.        {  
  9.            InitializeComponent();  
  10.            this.InitPhoto();  
  11.        }  
  12.        public List<Photo> photos = new List<Photo>();  
  13.   
  14.   
  15.        private void InitPhoto()  
  16.        {  
  17.   
  18.   
  19.            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();  
  20.            fbd.ShowDialog();  
  21.            string rootPath = fbd.SelectedPath;  
  22.            //MessageBox.Show(rootPath);  
  23.            GetAllImagePath(rootPath);  
  24.            lstImgs.ItemsSource = photos;  
  25.   
  26.   
  27.        }  
  28.         public void GetAllImagePath(string path)  
  29.        {  
  30.            DirectoryInfo di = new DirectoryInfo(path);  
  31.            FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);  
  32.   
  33.   
  34.            if (files != null && files.Length > 0)  
  35.            {  
  36.                foreach (var file in files)  
  37.                {  
  38.                    if (file.Extension==(".jpg") ||  
  39.                        file.Extension == (".png") ||  
  40.                        file.Extension == (".bmp") ||  
  41.                        file.Extension == (".gif"))  
  42.                    {  
  43.                        photos.Add(new Photo()  
  44.                        {  
  45.                            FullPath = file.FullName  
  46.                        });  
  47.                    }  
  48.                }  
  49.            }  
  50.        }  
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值