的WinForms数据绑定:可以一个类型转换器来代替格式/解析事件?转换器、绑定、类型、事件

2023-09-03 05:09:27 作者:爱已欠费

在一个WinForms形式,我想提供的视觉提示用户在输入字段包含无效值。为此,我想一个输入字段的标签前景色属性绑定到(布尔) IsPropertyValid 属性该标签会变成红色的基本模式,例如当 IsPropertyValid ==假

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;
}

如果我想这样做WPF中,我想我会指定一个自定义的值转换器(布尔颜色)直接与XAML的约束力。 最重要的是,我不会有指通过其名称的特定控制。的

If I wanted to do this in WPF, I suppose I would specify a custom value 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.

我希望做同样的事情与我的WinForms形式。理想情况下,我可以直接在窗体设计器指定一个特定的绑定类型转换器对象。这可能吗?

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?

推荐答案

我的previous答案(现已删除)是不正确的:这的可以的来完成,使用自定义的 类型转换器

My previous answer (now deleted) was incorrect: This can be done, using a custom TypeConverter.

首先,一个人需要一个合适的转换器。 (不像XAML,一个不实施的 的IValueConverter ,但是从的 类型转换器 ),例如:

First, one needs a suitable converter. (Unlike with XAML, one does not implement a IValueConverter, but derive from TypeConverter.) For example:

// using System;
// using System.ComponentModel;
// using System.Drawing;
// using System.Globalization;

sealed class BooleanToColorConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context,
                                      Type destinationType)
    {
        return destinationType == typeof(Color);
    }

    public override object ConvertTo(ITypeDescriptorContext context,
                                     CultureInfo culture,
                                     object value, 
                                     Type destinationType)
    {
        return (bool)value ? Color.Green : Color.Red;
    }
}

下一步(也不像XAML数据绑定),该转换器不适用于绑定的自身的;它必须使用的 [类型转换器] 页面属性:

Next, (and also unlike with XAML data binding,) this converter is not applied to the binding itself; it must be attached to the data source's property using the [TypeConverter] attribute:

// using System.ComponentModel;

partial class DataSource : INotifyPropertyChanged
{
    [TypeConverter(typeof(BooleanToColorConverter))] // <-- add this! 
    public bool IsValid { get { … } set { … } }
}

最后的格式必须在数据绑定启用:

Finally, formatting must be enabled on the data binding:

// Control control = …;
// DataSource dataSource = …;

control.DataBindings.Add("ForeColor", dataSource, "IsValid", formattingEnabled: true);
//                                                           ^^^^^^^^^^^^^^^^^^^^^^^

请注意,这个例子只是单向关系(数据源控制)数据绑定。对于双向数据绑定,你能还必须重写类型转换器。[灿]的ConvertTo 的方法。

Note that this example only deals with one-way (data source to control) data binding. For two-way data binding, you would additionally have to override the TypeConverter.[Can]ConvertTo methods.