Is it possible to databind 2 properties to 1 control? Specifically, I'm thinking phone numbers. I've got an object where all of the properties are bindable directly to 1 control, but there is an areacode and phonenumber property on the object. My users very much prefer a masked textbox for entering phone numbers as opposed to 2 separate ones.
Likewise, it's much easier to add the binding in the form load and call the persistent objects .save() method instead of populating the controls on load and re-setting them on save (that, and there's a logical disconnect between databound properties in the form code and non-bound ones).
So, I'm wondering if it's possible at all.
Edit> solved thanks to Yoooder.
The code that I wrote to solve this looks like this:
public class Person : PersistentBase
{
private string areaCode;
private string number;
public string AreaCode
{
get { return this.areaCode; }
set { Persist("AreaCode", this.AreaCode, value); } // pseudocode
}
public string Number
{
get { return this.number; }
set { Persist("Number", this.number, value); }
}
[NonPersistent]
public string PhoneNumber
{
get { return string.Format("{0}{1}", this.AreaCode, this.Number)); }
set
{
PhoneParts parts = SplitIntoParts(value); // uses regex etc.
// Validate full phone number
this.AreaCode = parts.AreaCode;
this.Number = parts.Number;
}
}
}
and I bind as I normally would
textBox1.DataBindings.Add(new Binding("Text", this.person, "PhoneNumber"));