2016-06-29 33 views
0

爲了使用ItemsControl綁定圖像源,我們需要使用哪種類型的列表。我有這樣一個ItemsControl:圖像類型ItemsControl上的錯誤

<ItemsControl x:Name="championPicControl" Margin="10,0,516,63"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Image Source="{Binding}" Height="100" Width="100"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

而且它還有我一個錯誤,當我做的:

 List<BitmapImage> list = new List<BitmapImage>(); 
     foreach(var i in summsList) 
     { 
      BitmapImage champ = new BitmapImage(); 
      champ.BeginInit(); 
      champ.UriSource = new Uri("Resources/championsSquare/" + i.championName + "_Square_0.png", UriKind.Relative); 
      champ.EndInit(); 
      list.Add(champ); 
     } 

     championPicControl.ItemsSource = list; 

我應該爲了得到它的工作怎麼辦列表的類型和什麼?

+1

你看到了什麼錯誤? – FishStix

+0

我在你的代碼中看到2個潛在的問題: 對於嵌入式資源,最好使用UriKind.Absolute'pack:// application:,,,/[YourAssemblyName]; component/Resources/{Y ourImageName} .' 需要使用ObservableCollection而不是List – Anton

回答

1

後面代碼中的資源文件URI必須全部爲Resource File Pack URIs。所以,你應該創建BitmapImages像

var champ = new BitmapImage(new Uri(
    "pack://application:,,,/Resources/championsSquare/" + i.championName + "_Square_0.png")); 

除此之外,它不是必要使用BitmapImages的名單。 WPF提供從string,Uribyte[]ImageSourceImage.Source屬性的類型)的自動類型轉換。

因此,您可以將IEnumerable<string>的資源URI字符串分配給ItemsControl的ItemsSource屬性。

championPicControl.ItemsSource = summsList.Select(
    i => "pack://application:,,,/Resources/championsSquare/" + i.championName + "_Square_0.png");