Reputation: 53
I'm currently dealing with a rather MVVM-unfriendly custom control that exposes a lot of it's "properties" through plain methods instead of dependency properties, or requires the properties to be set by a method because additional parameters need to be passed.
Simple pseudo example:
class TextControl : CustomControl {
void SetText(string text, int fontSize);
string GetText(string defaultValue);
}
Now I'd like to bind to the "Text" of the control, and just e.g. pass some defaults for the parameters, if needed. As for the "setter", I can just make an attached property and provide a callback which then invokes the SetText()
, but what about the "getter"? Is there a clean way to handle this or is the only way to pass the view to the view-model?
Upvotes: 1
Views: 218
Reputation: 13048
You could create attached properties to provide bindings, these properties would internally manipulate the "unfriendly" control.
One potential advantage is that attached properties can be be used on more than one control; so if you happened to have a family of 3rd party controls which shared some attributes in common, you might be able to write one set of attached properties to be used on all of them.
Upvotes: 1
Reputation: 31721
What you can do is to create your own custom control which will simply encapsulate the control in question. That way you can expose MVVM'able dependency properties, but in the code behind it manipulates and tweaks the third party control as needed.
Upvotes: 2