C# Winforms数据绑定:是否可以使用TypeConverter来代替格式化/解析事件?

C# Winforms数据绑定:是否可以使用TypeConverter来代替格式化/解析事件?,c#,.net,winforms,data-binding,typeconverter,C#,.net,Winforms,Data Binding,Typeconverter,在Winforms表单中,我希望在输入字段包含无效值时向用户提供可视提示。为此,我希望将输入字段标签的ForeColor属性绑定到基础模型的(布尔)IsPropertyValid属性,以便标签在IsPropertyValid==false时变为红色 我目前拥有的是用于绑定的格式事件的事件处理程序: Controls["dateOfBirthLabel"].DataBindings["ForeColor"].Format += convertBoolToColor; // (dateOfBirth

在Winforms表单中,我希望在输入字段包含无效值时向用户提供可视提示。为此,我希望将输入字段标签的
ForeColor
属性绑定到基础模型的(布尔)
IsPropertyValid
属性,以便标签在
IsPropertyValid==false
时变为红色

我目前拥有的是用于绑定的
格式
事件的事件处理程序:

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中的绑定指定一个自定义(
bool
Color
)。最重要的是,我不必通过名称引用特定控件

我想对我的Winforms表单做同样的事情。理想情况下,我可以直接在表单设计器中为特定绑定指定一个
TypeConverter
对象。这有可能吗?

不正确:这可以通过使用自定义设置来完成

c.DataBindings.Add("Checked", dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged );


class Someclass
{
[TypeConverter(typeof(IntBoolConverter))]
        public int p_isEnable
        {
            get { return nc_isEnable; }
            set { m_isEnable= value); }
        }
}
 public class IntBoolConverter:TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {

            if (sourceType == typeof(bool))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(int))
            {
                return true;
            }
            return base.CanConvertFrom(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is bool)
            {
                var objectToInt = value.ObjectToBool();
                return objectToInt;
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(bool))
            {
                return value.ObjectToBool();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
首先,需要合适的转换器。(与XAML不同,我们不实现,而是从中派生。)例如:

// 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数据绑定不同,此转换器不应用于绑定本身;必须使用以下命令将其附加到数据源的属性:


请注意,此示例仅处理单向(控制数据源)数据绑定。对于双向数据绑定,您还必须重写
TypeConverter.ConvertFrom
typeconvertfrom
方法。

请您解释一下这段代码是如何工作的,以及它是如何回答问题的?如果我知道的足够多,可以提出正确的问题。现在我明白了,这很容易找到。@RogerWillcocks:我在学习新东西时经常遇到同样的问题。如果您询问您的问题,可能会对其他人有所帮助,但请按照您最初的措辞,然后在此处将其标记为此问题的副本(或添加注释“与…”),以帮助人们找到解决方案。
// using System.ComponentModel;

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

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