本文介绍了请问,如何使图像列表元素出现在按钮数组上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Ive tried these, but ain't working...
ArrayList list = new ArrayList();
            try
            {

                DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) );
                FileInfo[] file = dir.GetFiles();

                foreach (FileInfo fl in file)
                {
                    if (fl.Extension == ".jpg")// || fl.Extension == ".jpeg" || fl.Extension == ".png" || fl.Extension == ".gif")
                    {
                        list.Add(fl);
                    }

                }

                lbltotal.Content = "Photo Viewer(" + list.Count + ")";
                Button[] btn = new Button[list.Count];
                Image[] img = new Image[list.Count];
                DockPanel[] dock = new DockPanel[list.Count];

                Label[] lbl = new Label[list.Count];

                                for (int i = 0; i < btn.Length; i++)
                                {
                                    btn[i] = new Button();
                                    img[i] = new Image();
                                    lbl[i].Content = list[i].ToString();
                                    img[i].Source = new BitmapImage(new Uri(list[i].ToString()));
                                    btn[i].Width = 180;
                                    btn[i].Height = 150;
                                    btn[i].BorderThickness = new Thickness(2, 2, 2, 2);
                                    btn[i].Margin = new Thickness(0, 5, 5, 0);
                                    //dock[i].Children.Add(img[i]);
                                    //dock[i].Children.Add(lbl[i]);


                                    //btn[i].Content = img[i];
                                    //stack[i].Children.Add(img[i]);
                                    //btn[i].Content = dock[i];
                                    wrapContainer.Children.Add(btn[i]);
                                }
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message);
            }

推荐答案

//i created the URI object itself and passed in the FileInfo object in each uri in the foreach loop
//you can replace the loop as 
                        uri = new Uri(fl.FullName);
                        list.Add(new BitmapImage(uri));
//after you might have created a Uri class
//then replace the for loop part with these
 btn[i] = new Button
                                    {
                                        Width = 180,
                                        Height = 150,
                                        BorderThickness = new Thickness(2, 2, 2, 2),
                                        Margin = new Thickness(0, 5, 5, 0)
                                        /*
                                        Content = new Image
                                        {
                                            Source = list[i],
                                            VerticalAlignment = System.Windows.VerticalAlignment.Center
                                        }*/
                                    };


                                     dock[i] = new StackPanel();
                                     img[i] = new Image();
                                     img[i].Source = list[i];
                                     dock[i].Children.Add(img[i]);
                                     lbl[i] = new Label();
                                     lbl[i].Content = list[i].ToString();
                                     dock[i].Children.Add(lbl[i]);
                                     btn[i].Content = dock[i];
                                     wrapContainer.Children.Add(btn[i]);


这篇关于请问,如何使图像列表元素出现在按钮数组上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 08:21