2010-08-07 18 views
6

在Winforms表单中,我想在输入字段包含无效值时向用户提供可视提示。为此,我想将输入字段标签的ForeColor属性绑定到基础模型的(布尔型)IsPropertyValid属性,使标签在IsPropertyValid == false时变为红色。Winforms数据绑定:可以使用TypeConverter而不是Format/Parse事件吗?

我现在有是绑定的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 converterboolColor)直接与绑定在XAML中。 最重要的是,我不必通过名称来引用特定的控件。

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

回答

5

My previous answer (now deleted)是不正确的:这可以来完成,使用自定义TypeConverter

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

// 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] attribute数据源的属性:

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

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

注意,这个例子只单向交易:

// using System.ComponentModel; 

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

最后formatting必须在数据绑定启用(数据源来控制)数据绑定。对于双向数据绑定,您还需要覆盖TypeConverter.ConvertFromTypeConverter.CanConvertFrom方法。

-3
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); 
     } 
    } 
+0

您能否解释一下这段代码应该如何工作,以及它如何回答这个问题? – stakx 2014-06-18 11:56:43

相关问题