In a Winforms form, I want to provide visual cues to the user when an input field contains an invalid value. To that end, I want to bind the ForeColor
property of a input field's label to the (boolean) IsPropertyValid
property of the underlying model such that the label turns red when IsPropertyValid == false
.
What I currently have is an event handler for the binding's Format
event:
Controls["dateOfBirthLabel"].DataBindings["ForeColor"].Format += convertBoolToColor;
// (dateOfBirthLabel.ForeColor is bound to a boolean IsDateOfBirthValid property.)
void convertBoolToColor(object sender, ConvertEventArgs e)
{
e.Value = (bool)e.Value ? Color.Black : Color.Red;
}
If I wanted to do this in WPF, I suppose I would specify a custom type converter (bool
to Color
) directly with the binding in the XAML. Most importantly, I wouldn't have to refer to a specific control via its name.
I would like to do the same thing with my Winforms form. Ideally, I could specify a TypeConverter
object for a particular binding directly in the Forms Designer. Is this possible?