none
Binding an Entity (not an Entity property) to a ValueConverter value

    Question

  • I need to bind a whole Entity to a ValueConverter (via a DataContext) so I can perform my converter logic based on multiple properties of the Entity as well as a property of a "child" Entity collection. While I am able to get this to pass in and convert property at the instantiation of the bound control, changing a value of a property does not update the bound control (checking in debugging shows I do not ever actually drop into the converter method once I update the property value)

    This appears to be because there is no such thing as an EntityChanged() method, only a PropertyChanged() so the converter is never aware of a property change or a need to execute the converter method.

    I have tried binding the value to one property and binding the parameter to the entity as a whole, but you cannot currently bind objects to the parameter.

    Monday, January 30, 2012 10:57 AM

Answers

  • Value converters and the binding system were not designed to handle this kind of problem by themselves (monitor a whole bunch of properties and colllections, then update one control when any of them change).  Generally the binding system is designed to monitor one collection or property per binding.

    The good news is that this is exactly the kind of thing that is appropriate to do in your model or view model.  Create a calculated property that does the equivalent of what you are doing in your value converter.  If it is specific to a particular UI implementation, put it in your view model.  Otherwise put it in the entity or a dedicated model class.  Monitor the collection changed and property changed events that can affect the calculated value and raise property changed for the calculated property when one of those monitored events fires.

    One more thing: think carefully about when you need to unsubscribe the monitored events and be sure to unsubscribe them reliably to avoid memory leaks.

    Monday, January 30, 2012 1:50 PM