1

I have a ProductViewModel class which contains different properties. Then I have a ProductDetailsViewModel class which inherit from ProducViewModel class. The reason I am doing it this way is in order to get correct binding environement and avoid duplication of properties from previous view.

I am allowed to do this or each ViewModel should be clearly isolated?

Through code I can acess the properties of the ProductViewModel class from ProductDetailsViewModel view but when I set the datacontext of my ProductDetailView to ProducDetailsViewModel class and bind properties URI for instance which is define inside the inherited class, binding seems not occurs.

Any idea ?

serge
  • 391
  • 5
  • 24

2 Answers2

1

You can do this too, but i think maybe better would be to separate them and use Dependency Injection.

You create and interface for your ProductViewModel and implement it and then you inject this into your ProductDetailsViewModel.

MVVM + WPF + DI

MSDN DI

Community
  • 1
  • 1
BigL
  • 1,631
  • 1
  • 11
  • 10
  • hmmm do you have sample, never done that and work with those Dependency injection – serge Jan 02 '12 at 14:45
  • Sadly now i don't have a sample for that, but Google is your friend too and there are a lot of examples to learn from. You could do your DI manually or with a framework too. Unity Prism would be nice to take a look at but to start i think they would be a bit complex to understand. – BigL Jan 02 '12 at 15:47
1

Yes this is fine, and I do this all the time in my WPF projects so it should just work. Some suggestions:

  • Can you check your output window when debugging the application. Are there any binding errors suggesting a mis-typed xaml binding?
  • Are you using any DataTemplates in xaml which bind to a specific type, e.g. ProductViewModel not ProductDetailsViewModel?
  • Does the base type (ProductViewModel) implement INotifyPropertyChanged?
  • Are all the properties in ProductViewModel and ProductSetailsViewModel raising the PropertyChanged event with directly typed string property name?

Best regards,

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • Yes I get binding errors when doing it because the DataContext seems not position properly. – serge Jan 02 '12 at 14:37
  • for a better details I have post it here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/6a1e2ddf-1eec-4339-9174-659de61f1d4b – serge Jan 02 '12 at 14:46
  • Your binding fails because your user control doesn't have an ancestor which is a "vm:ProductViewModel". The UserControls ancestor is his parents in the Visual Tree, so you should look for an ancestor higher in the Tree which DataContext is set to ProductViewModel. – BigL Jan 02 '12 at 15:52
  • As BigL correctly points out, this line is likely causing your problem: . Just as an aside, have you heard of the WPF tool called Snoop (snoopwpf.codeplex.com)? I'd strongly advise learning to use this to debug binding and visual tree problems in WPF. – Dr. Andrew Burnett-Thompson Jan 02 '12 at 17:30
  • This what I was expected, but I though that the Ancestor was looking up to the tree until in finds correct type which seems not to be the case. how can I specify then here in order to tell it to look until it finds it ? – serge Jan 02 '12 at 20:11
  • I don't think you need the RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=vm:ProductViewModel} part at all. The style with key MediaCount is applied to the StackPanel. So long as the StackPanel's Datacontext is equal to your ProductDetailsViewModel instance then the trigger binding should just work. Best bet would be to download Snoop as learn how to use that to inspect elements and triggers. its also fantastic at debugging binding errors – Dr. Andrew Burnett-Thompson Jan 02 '12 at 20:32