代码之家  ›  专栏  ›  技术社区  ›  stakx - no longer contributing Saravana Kumar

Winforms数据绑定:是否可以使用TypeConverter来代替Format/Parse事件?

  •  6
  • stakx - no longer contributing Saravana Kumar  · 技术社区  · 14 年前

    ForeColor 输入字段的标签的属性(布尔值) IsPropertyValid IsPropertyValid == false .

    我目前拥有的是一个用于绑定 Format

    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中这样做,我想我应该指定一个自定义 value converter bool Color )直接使用XAML中的绑定。 最重要的是,我不必通过名称引用特定控件。

    TypeConverter

    2 回复  |  直到 10 年前
        1
  •  8
  •   Community CDub    7 年前

    My previous answer (now deleted) 不正确:这 使用自定义 TypeConverter

    第一, 一个人需要一个合适的转换器(与XAML不同,它不实现 IValueConverter ,但从 类型转换器

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

    接下来, 它本身 ; 必须使用 [TypeConverter] attribute :

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

    最后 , formatting

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

    TypeConverter.ConvertFrom TypeConverter.CanConvertFrom

        2
  •  -3
  •   laaposto    10 年前
    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);
            }
        }