1
votes

Basically I have an object in my view model that contains an ObservableCollection of a custom object. My XAML's DataContext is set to my ViewModel, my ViewModel contains a 'Scratchdisk' object, and the Scratchdisk object contains an ObservableCollection of Frame objects. Both the Scratchdisk and the Collection are set up as DependencyProperties.

In short: XAML --DataContext--> EditorViewModel --DependencyProperty--> Scratchdisk --DependencyProperty--> ObservableCollection<Frame>

The Frame object has 3 standard properties: Index, Image, and ImageUrl.

I'm trying to bind to the ObservableCollection in my XAML using this code:

<ItemsControl DataContext="{Binding Source=ThumbnailScratchdisk}" ItemsSource="{Binding Frames, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ThumbnailTemplate}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal"></VirtualizingStackPanel>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
</ItemsControl>

Where ThumbnailTemplate is defined in Window Resources as:

<DataTemplate x:Key="ThumbnailTemplate">
    <Image Width="128" Height="96" Source="{Binding ImageUrl}"/>
</DataTemplate>

Theoretically, what should happen is, the Scratchdisk should receive filenames, create Frame objects, add them to the Collection, and then the binding should display them. The ObservableCollection is working and being populated, but the binding doesn't seen to be updating. All the updatable properties are set as DependencyProperties so the binding should update shouldn't it?

Links to the files:

XAML

ViewModel

Scratchdisk

Frame

2
Please post the viewmodel code which has observable collection. Please post the complete hierarchy of your top level datacontext. I feel your binding with the Frames object but your datacontext is viewmodel. You need to navigate to ViewModel.ScratchObject.FramesPiyush Parashar
@PiyushParashar I've linked the files at the bottom of the post, they're fairly big files so I didn't want to quote them in the question.Callum Booth
You need to notify the change of any properties in your Frame, e.g. ImageUrl to your View. You can test it by giving a default value of ImageUrl in the Frame class, if you can now see this default value, then you know the binding is working, just not updated when it changes in the VM.Bolu
Please put some more effort in your question , your viewmodel with the observablecollection , where you update it , what ever 'ThumbnailScratchdisk' is and only relevant xaml. no one want's to dl your stufferan otzap

2 Answers

4
votes

The problem is in the binding of the DataContext of your ItemsControl. You're setting it to "{Binding Source=ThumbnailScratchdisk}", but what you (presumably) want is to set it to just "{Binding ThumbnailScratchdisk}".

The DataContext of the page is already an instance of EditorViewModel, and you want the DataContext for the ItemsControl to bind to the property ThumbnailScratchdisk of that viewmodel.

0
votes

Trying changing the binding path in XAML to ThumbnailScratchdisk.Frames