views:

895

answers:

3

I have a question regarding WPF binding and converting the data types seen by the UI objects in XAML.

I have a user control that I would like to reuse in different applications. The user control displays a thumbnail image and several TextBlocks to display person demographic information such as name and address. The user control is used in an MVVM design, so it’s bound to a ViewModel specific to the user control.

Following typical MVVM design principles, The ViewModel for the user control is often embedded in other ViewModels to make up a larger UI.

The user control view model expects a certain type (class) as its binding object. However, the ViewModels in which the UC’s VM in embedded have entirely different object models, so they cannot simply pass-through their data to the UC’s VM. There needs to be a conversion of the parent VM’s data model to the UC VM’s data model.

My question is this: Is there a sanctioned way to perform this conversion?

I looked at IValueConverter and IMultiValueConverter and these do not look like the way to go.

I guess what I need is a sort of shim between the parent VM and the embedded UC VM where the parent VM’s data is converted to the format required by the UC VM.

Or, does it basically come down to I have to write a custom UC VM to handle whatever types the parent VM provides?

+1  A: 

If the parent VM is a superset of the child VM, normally the parent VM would just hold a reference to the child VM. It would expose that reference as a property, and you would bind a ContentControl (or whatever) to that property.

Would this not solve your problem?

HTH, Kent

Kent Boogaart
Thanks, that would work fine if the parent VM supplies data in the native format of the UC VM. In other words, yes, the parent VM is a superset of the child VM, but the data is in an entirely different format. I am new to this stuff so you will have to forgive my ignorance.
MattJ
A: 

If you really want and need to do type conversions, the value converters are exactly what you want to use. That said, typically the type of conversions handled by things like IValueConverter are relatively simple and direct.

If, however, your top-level/parent/management user control needs to parse off bits and pieces of some larger type to the user controls which host its actual content, then that is the job of that top level control. Don't get all wrapped up in doing all of this in XAML. It's perfectly fine to parse off what you need and set these child control properties directly.

Ken Wootton
+2  A: 

I agree with Ken, I think he has the answer. If you have n many configurations of your data that you want to pass into a common user control, then you want the owner of that configuration of data to convert it into a common form to be bound to the user control.

Each view that uses the control would have a corresponding view model that exposes a property in a common format:

public class SampleViewModel {
   ...

    IUserControlData ControlData 
    { 
       get 
       {
          // Do conversion here or do it early and cache it.
       }
    }

   ...
}

Then you would bind that property to your user control in the view

<common:CommonUserControl DataContext={Binding Path=ControlData} ... >
MichaC