Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 2.62 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Take image from list using PK and show as part of another object in a ListBox in MVVM
  2. <ListBox ItemsSource="{Binding AllCards}">
  3.     <ListBox.ItemTemplate>
  4.         <DataTemplate>
  5.             <StackPanel Orientation="Horizontal">
  6.                 <Image Width="100" Height="100" Source="{Binding }" />
  7.                 <TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Width="300" Text="{Binding CardName}" />
  8.             </StackPanel>
  9.         </DataTemplate>
  10.     </ListBox.ItemTemplate>
  11. </ListBox>
  12.        
  13. public class Card
  14. {
  15.     public int Id { get; set; }
  16.     public string CardName { get; set; }
  17.     public int Image1 { get; set; }
  18.     public int Image2 { get; set; }
  19.     public int Image3 { get; set; }
  20. }
  21.        
  22. public class Image
  23. {
  24.     public int ImageId { get; set; }
  25.     public string Imagepath { get; set; }
  26.     public string ImageName{ get; set; }
  27. }
  28.        
  29. public class Card
  30. {
  31.     public int Id { get; set; }
  32.     public string CardName { get; set; }
  33.     public BitmapImage Image1 { get; set; }
  34.     public BitmapImage Image2 { get; set; }
  35.     public BitmapImage Image3 { get; set; }
  36. }
  37.        
  38. <ListBox ItemsSource="{Binding AllCards}">
  39.     <ListBox.ItemTemplate>
  40.         <DataTemplate>
  41.             <StackPanel Orientation="Horizontal">
  42.                 <Image Width="100"
  43.                        Height="100"
  44.                        Source="{Binding Image1}" />
  45.                 <TextBlock
  46.                        VerticalAlignment="Center"
  47.                        Margin="10,0,0,0"
  48.                        Width="300"
  49.                        Text="{Binding CardName}" />
  50.             </StackPanel>
  51.         </DataTemplate>
  52.     </ListBox.ItemTemplate>
  53. </ListBox>
  54.        
  55. public class Card
  56. {
  57.     public int Id { get; set; }
  58.     public string CardName { get; set; }
  59.     public int Image1Id { get; set; }
  60.     public int Image2Id { get; set; }
  61.     public int Image3Id { get; set; }
  62. }
  63.        
  64. public BitmapImage Import(string path, bool isRelativePath)
  65. {
  66.     var fullPath = GetFullPath(path, isRelativePath);
  67.     var buffer = File.ReadAllBytes(fullPath);
  68.     MemoryStream stream = new MemoryStream(buffer);
  69.     var bitmapImage = new BitmapImage();
  70.     bitmapImage.BeginInit();
  71.     bitmapImage.StreamSource = stream;
  72.     bitmapImage.EndInit();
  73.     return bitmapImage;
  74. }
  75.  
  76. public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
  77. {
  78.     var stream = new MemoryStream();
  79.     bitmap.Save(stream, ImageFormat.Png);
  80.     var bitmapImage = new BitmapImage();
  81.     bitmapImage.BeginInit();
  82.     bitmapImage.StreamSource = stream;
  83.     bitmapImage.EndInit();
  84.     return bitmapImage;
  85. }
  86.  
  87. public string GetFullPath(string path, bool isRelativePath)
  88. {
  89.     return isRelativePath ? FolderPaths.Application + @"" + path : path;
  90. }