1 public override void OnApplyTemplate() 2 { 3 base.OnApplyTemplate(); 4 5 string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG"; 6 string[] ImageType = imgtype.Split('|'); 7 8 for (int i = 0; i < ImageType.Length; i++) 9 { 10 string[] dirs = Directory.GetFiles(@"e:\\Images", ImageType[i]); //dirs为绝对路径 11 foreach (string dir in dirs) 12 { 13 Image img = new Image(); 14 BitmapImage bitmap = new BitmapImage(new Uri(dir, UriKind.Absolute)); //以绝对路径形式设置Uri 15 img.Source = bitmap; 16 img.Height = 150; 17 img.Margin = new Thickness(1); 18 this.wp2.Children.Add(img); 19 } 20 } 21 }