Silverlight converter to convert an object to a string and back
I have an Address object in my model which has 6 address lines as separate properties. In my Silverlight view I want this to be displayed in a multiline text box and to be updated using databinding.
The View is linked to a ViewModel which has an Address property which is always set. My first thought was to use a ValueConverter which was initially fine as it could take an Address object and pass back a string which is displayed.
My problem arises when I want to ConvertBack the string. I wish to update the existing Address object but cannot find a way to do this. I think开发者_Python百科 you can pass back a new Address object but this is not what I want to happen.
I am on the right track here or is there a better method than using a ValueConverter.
You are almost on the right track here! You are right in your observation that the ConvertBack
method would need to create a new Address instance. You cannot obtain a reference back to the original address in your converter, the binding framework does not permit this kind of tight coupling.
How about using MVVM? create a ViewModel that exposes / adapts your Address as a string, basically performing the role of your value converter. When the TwoWay
binding updates this string property, your ViewModel can then update the Address that it adapts.
精彩评论