Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have a query and I'm sure I've had the issue before yet the results from googling are bringing up the same thing which I have already implemented (in a fashion).

I have a MVVM application and one of the V-VM's is to display some file listings.

The viewModel has a read only observable collection property which looks like this:

C#
public ReadOnlyObservableCollection<TemplateFileViewModel> ListOfTemplates
{
     get
     {
         m_FileListing = new ObservableCollection<TemplateFileViewModel>();
         if (!String.IsNullOrWhiteSpace(TemplateDirectoryPath))
         {
              foreach (var s in Directory.GetFiles(TemplateDirectoryPath, "*.MyEXT", 
                          IncludeSubFolders ? SearchOption.AllDirectories : 
                          SearchOption.TopDirectoryOnly))
               {
                    m_FileListing.Add(new TemplateFileViewModel(s));
               }
         }

         return new ReadOnlyObservableCollection<TemplateFileViewModel>(m_FileListing);

    }
}


I also have a property "TemplateDirectoryPath" which looks like this:
C#
public string TemplateDirectoryPath
{
     get { return m_TemplateDirectoryPath; }
     set
     {
         if (Equals(m_TemplateDirectoryPath, value) || !Directory.Exists(value)) return;
         m_TemplateDirectoryPath = value;
         OnPropertyChanged("TemplateDirectoryPath");
         OnPropertyChanged("ListOfTemplates");
     }
}


The idea is that when the TemplateDirectoryPath is updated it triggers a list box to refresh its contents, which is bound to the ListOfTemplates property on the view model.

No my understanding is that if I user the "OnPropertyChanged" event it should result in the view doing a data refresh.

My listbox is bound as follows:

XML
<ListBox Grid.Column="0" ItemsSource="{Binding Path=ListOfTemplates, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource TemplateFileDataTemplate}"/>


Why would this not be triggering an update on the screen? From what I can see it all appears correct, am I missing something really obvious?


Thanks
Posted

1 solution

I found my Issue,

My ViewModel was inheriting from a base class that implemented INotifyPropertyChanged, however I had to declare my viewModel as such:

C#
internal class myVM: myBaseVM, INotifyPropertyChanged


and ensure the correct references were in place else it failed to trigger the events. Because my base class already implemented the methods/events for INotifyPropertyChanged, I did not have to duplicate their implementation.
 
Share this answer
 
Comments
Justin Carper 20-Sep-18 10:26am    
++ This solved my issue. I can't believe you still have to include the interface even though it's handled in the base class, but oh well, solved the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900