0

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?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
mins
  • 6,478
  • 12
  • 56
  • 75
  • I realize `BitmapImage` isn't a `FrameworkElement`, and therefore the binding shouldn't be able to use `DataContext` [without a converter](https://stackoverflow.com/a/20617/774575). I'm investigating this deeper, but at the moment still don't understand why the second solution displays images. – mins Nov 29 '19 at 23:31
  • It's not just that it's not a `FrameworkElement`, but also that it's not in the visual tree at all. That's the part that actually causes the error (i.e. even if it were a `FrameworkElement`, not being in the tree would still be a problem). Seems like you've found work-arounds. Are you just asking why your images still display in spite of the error? Unfortunately, there are some corner cases like this where an error results but things still work. – Peter Duniho Nov 29 '19 at 23:43
  • Why don't you use a property of type `ImageSource` in the PictureItem class (and hence stick with ``? Then assign a BitmapSource with CacheOption OnLoad to that property. – Clemens Nov 29 '19 at 23:49
  • @PeterDuniho: I'd like to better understand the concept of visual tree (I read [MS](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/trees-in-wpf), but it still a bit fuzzy, including the relationship with DataContext, and how it prevents the binding). – mins Nov 30 '19 at 01:15
  • @Clemens: I've done this this way. Thanks. – mins Nov 30 '19 at 01:39

0 Answers0