Reputation: 10156
I have some problems with binding my ListView
to ObservableCollection<Bitmap>
...
It's my XAML
:
<ListView ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=FrameImages}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel
Width="Auto"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Border Width="100" Height="75" BorderThickness="1" BorderBrush="DarkGray" VerticalAlignment="Center" Margin="7,5,7,5">
<Image Margin="5,5,5,5" Width="100" Height="75" Source="{Binding}" Stretch="Fill"></Image>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
public ObservableCollection<Bitmap> FrameImages { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
FrameImages = new ObservableCollection<Bitmap>();
Bitmap img = new Bitmap(@"E:\test\3047817.bmp");
FrameImages.Add(img);
}
When I add the element, it seems to appear in ListView
, but it's transparent (just an empty frame):/ I tried to save the bitmap back into file and there were no problems (same as the original one). I don't know why it's not working:(
[edit]
Btw. my code works if I replace ObservableCollection<Bitmap>
with ObservableCollection<BitmapSource>
. But here's the additional conversion which affects on program's performance... That's why I need Bitmap
.
Upvotes: 0
Views: 1708
Reputation: 128098
You can't use System.Drawing.Bitmap that way in a WPF application. The class does not belong to WPF. It encapsulates a GDI+ bitmap, whereas WPF is based in DirectX. Hence you need to use BitmapSource.
You might however simply bind the ItemsSource property of your ListView to a collection of image path strings. The necessary conversion from string to ImageSource is performed automatically by WPF.
If you really need to manually create the bitmaps, you should define your collection as ObservableCollection<ImageSource>
and create collection elements by something like this:
var img = new BitmapImage(new Uri(@"E:\test\3047817.bmp"));
FrameImages.Add(img);
You might want to take a look into Imaging Overview. And you should make yourself familiar with the BitmapSource class hierarchy in WPF.
Upvotes: 2