您的位置:首页 > 其它

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  

                       });  

                   }  

               }  

           }  

       }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: