2 Replies - 5748 Views - Last Post: 21 November 2013 - 03:07 PM Rate Topic: -----

#1 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Data Binding to Inherited Class Members

Posted 19 November 2013 - 06:22 AM

Hello DreamInCode

I apologize for the lengthy pose in advance. I am currently exploring the strengths of WPF and the MVVM architecture. Working on a simple project I stumbled upon an issue where I cannot quite seem to find the 'elegant' solution.

The application is essentially a simple inventory management system for a generic shop selling various items. Currently I have an abstract Item base class and 2 derived classes: Bike and Shirt. The Bike class has a WheelType property while the Shirt class has a Color property.

As of now items are stored and shown in a ListBox and whenever the user clicks an item a panel shows information about the item. My issue lies in showing (a Label with the property name and a TextBlock with the property value) the properties unique to the derived classes. The solutions I have considered are:

  • Exposing the property through the ViewModel in a generic AdditionalInfo property

This is the solution I implemented for now however it does not feel very flexible and I feel it might be clunky if additional unique properties are added.

  • Exposing the property through some virtual method or property in the Model (Item class)

Trying to keep my Model as simple and generic as possible I feel this solution would be a pretty 'bad design'.

  • Have the View determine what information to look for depending on item type

I have a feeling that the best solution is somewhere in here but I am struggling to find a good way to do it without having a lot of C# code in the View which I am trying to avoid. I may be missing some clever Data Binding tricks.

Any input or references to helpful learning resources on this matter is greatly appreciated. I tried searching around for similar scenarios but did not find anything helpful.

Thank you in advance
Sigurd

Is This A Good Question/Topic? 0
  • +

Replies To: Data Binding to Inherited Class Members

#2 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Data Binding to Inherited Class Members

Posted 19 November 2013 - 08:45 AM

This is probably outside of normal use, because an object of a derived class is an object of the base class too. However, you could get the unique property names like so:

var baseProperties = typeof(BaseClass).GetProperties().Select(prop => prop.Name);
var derivedProperties = typeof(DerivedClass).GetProperties().Select(prop => prop.Name);
var uniqueDerived = derivedProperties.Where(derived => !baseProperties.Contains(derived));

Was This Post Helpful? 0
  • +
  • -

#3 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Re: Data Binding to Inherited Class Members

Posted 21 November 2013 - 03:07 PM

Thank you for the response MrShoes. That is a pretty nifty combination of reflection and LINQ. I will make sure to remember it.

For now I started doing some reading on Priority Bindings which seem like they might actually provide a pretty clean solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1