1

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

Callum Booth
  • 377
  • 4
  • 15
  • 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.Frames – Piyush Parashar Nov 25 '14 at 16:05
  • @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 Nov 25 '14 at 16:11
  • 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 Nov 25 '14 at 16:23
  • Side comment: [don't use DependencyObject as VM base](http://stackoverflow.com/q/291518/1997232). – Sinatr Nov 25 '14 at 16:23
  • 1
    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 stuff – eran otzap Nov 25 '14 at 16:24
  • *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*... we do *not* use `DependencyProperty`s in view models as they are UI elements... just use standard CLR type properties in your class. – Sheridan Nov 25 '14 at 16:25
  • @eranotzap I linked to the files because they're too long to quote in the question. Nowhere did I ask anyone to dl anything. – Callum Booth Nov 25 '14 at 16:28
  • @Sheridan If I change the DP's to standard properties, will I still be able to two way bind to them? – Callum Booth Nov 25 '14 at 16:28
  • Why you're using a `DependencyProperty` in your ViewModel? That seems unusual to me. A DP is basically just a property definition, and is typically used when the data for the property will be supplied by someplace else that this class won't have reference to. That said, it sounds like you're misunderstanding how the `DataContext` works a bit - it's the data item backing the control that the control's bindings use. I wrote a blog post on this for WPF beginners if you're interested: [What is this "DataContext" you speak of?](http://rachel53461.wordpress.com/2012/07/14/) – Rachel Nov 25 '14 at 16:33
  • @CallumBooth, using CLR properties in a non UI class is practically the same as using `DependencyProperty`s in a UI class, but `DependencyProperty`s should only be used in UI classes. – Sheridan Nov 25 '14 at 16:35
  • @Rachel Thanks for that link! I think I must've got confused when reading about DependencyProperties for the first time. I assumed that any property that's being bound to needs to be a DP, but obviously this isn't the case presumably. – Callum Booth Nov 25 '14 at 16:41
  • @CallumBooth It is true that if you are going to be binding a property in XAML, it has to be a DependencyProperty. For example, `TextBox.Text` has to be a DependencyProperty so we can set it to a binding. This allows WPF's binding system to get and set the property using the property definition defined for it. However the value you are binding *to* does not need to be a DP. That can be any regular public property. – Rachel Nov 25 '14 at 18:29

2 Answers2

4

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.

Willem van Rumpt
  • 6,490
  • 2
  • 32
  • 44
0

Trying changing the binding path in XAML to ThumbnailScratchdisk.Frames

Piyush Parashar
  • 866
  • 8
  • 20
  • Sorry, this didn't work. I thought this would've been implied though since the ItemsControl's DataContext was set to ThumbnailScratchdisk – Callum Booth Nov 25 '14 at 16:19
  • Yes I realized that as soon I posted it :( I can not find SetValue method in ScratchObject class. Where is it and what is the code? – Piyush Parashar Nov 25 '14 at 16:21