I've a working solution to display images items from files using a ListBox
:
<ListBox ItemsSource="{Binding Path=Pictures}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:PictureCollectionPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Width="100" Source="{Binding FullPath}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Pictures
is a collection derived from ObservableCollection<PictureItem>
with PictureItem
holding the file path:
class PictureItem {
...
public string FullPath { get; set; }
}
This solution has a drawback: The image files used in the items are locked.
In order to not lock the files, I've changed the item template to use BitmapImage
with its CacheOption="OnLoad"
:
<DataTemplate>
<Image Width="100">
<Image.Source>
<BitmapImage UriSource="{Binding FullPath}" CacheOption="OnLoad"/>
</Image.Source>
</Image>
</DataTemplate>
This also works, but now I get a message in the debug window:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=FullPath; DataItem=null; target element is 'BitmapImage' (HashCode=3088388); target property is 'UriSource' (type 'Uri')
I read some answers here, and a few posts about this message, which is assumed to indicate the DataContext
is not inherited by some element, preventing the binding to be resolved. However in my case, the binding seems to work, the images are shown normally, and on the other hand I don't see which element in the tree is not a FrameworkElement
.
What am I failing to understand?