- Take image from list using PK and show as part of another object in a ListBox in MVVM
- <ListBox ItemsSource="{Binding AllCards}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <Image Width="100" Height="100" Source="{Binding }" />
- <TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Width="300" Text="{Binding CardName}" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- public class Card
- {
- public int Id { get; set; }
- public string CardName { get; set; }
- public int Image1 { get; set; }
- public int Image2 { get; set; }
- public int Image3 { get; set; }
- }
- public class Image
- {
- public int ImageId { get; set; }
- public string Imagepath { get; set; }
- public string ImageName{ get; set; }
- }
- public class Card
- {
- public int Id { get; set; }
- public string CardName { get; set; }
- public BitmapImage Image1 { get; set; }
- public BitmapImage Image2 { get; set; }
- public BitmapImage Image3 { get; set; }
- }
- <ListBox ItemsSource="{Binding AllCards}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <Image Width="100"
- Height="100"
- Source="{Binding Image1}" />
- <TextBlock
- VerticalAlignment="Center"
- Margin="10,0,0,0"
- Width="300"
- Text="{Binding CardName}" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- public class Card
- {
- public int Id { get; set; }
- public string CardName { get; set; }
- public int Image1Id { get; set; }
- public int Image2Id { get; set; }
- public int Image3Id { get; set; }
- }
- public BitmapImage Import(string path, bool isRelativePath)
- {
- var fullPath = GetFullPath(path, isRelativePath);
- var buffer = File.ReadAllBytes(fullPath);
- MemoryStream stream = new MemoryStream(buffer);
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = stream;
- bitmapImage.EndInit();
- return bitmapImage;
- }
- public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
- {
- var stream = new MemoryStream();
- bitmap.Save(stream, ImageFormat.Png);
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = stream;
- bitmapImage.EndInit();
- return bitmapImage;
- }
- public string GetFullPath(string path, bool isRelativePath)
- {
- return isRelativePath ? FolderPaths.Application + @"" + path : path;
- }