views:

40

answers:

1

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?

A: 

OK, it seems that what I'm asking for is not possible with Winforms.

I've researched some more on MSDN, and I've only found some ways to convert objects to/from specific string representations using format providers. (That's what the Format property visible in the Forms Designer seems to be for.)

However, it seems that Winforms doesn't provide the means to convert data-bound values to/from arbitrary types, at least not through the Forms Designer. You're left with a binding's Format and Parse events, which can be used to that end, but cannot be set up in the Forms Designer.

stakx