I am looking for a solution to bind a property of a windows forms control (Text of a button or a label) to multiple properties of one (or more) objects via a formatting string. Basically, the displayed text on a button should look like "static text $1 more static text $2" where $1 is bound to the to the property of an object and $2 is bound to a different property of the same or a different object. Is there an easy way to accomplish that?
A:
You could encapsulate Property1 and Property2 in a third property that takes and returns the formatted string.
public string EncapsulatingProperty
{
get { return "static text" + property1 + "more" + property2; }
set { /* Parse the static text into the two variables */ }
}
Eric J.
2010-02-17 07:49:10
you'd better use string.Format
jmservera
2010-02-17 08:23:04
Unfortunately, the "static text" is not known to the object. I want to be able to define it at the time I establish the binding. I could, of course, implement a property or method that allows me to pass the static text to the object, but that wouldn't work if $1 is bound to the property of a different object than the one $2 is bound to.I thought of writing a "Combiner"-class which is bound to the properties of the (two) objects and has a property that returns the formated string, but I was hoping that there may be an existing solution for something like that.
Jan Traenkner
2010-02-17 08:25:52
A:
To add on Eric's response, Understanding Simple Data Binding can provide a good read on the subject.
Marcel Gheorghita
2010-02-17 08:03:40
A:
IF you are dealing with WPF/Silverlight for the UX, I believe you can write a ValueConverter to handle the proper displaying of the data... and in some instances, convert the values back.
Richard B
2010-02-17 09:15:58