Winforms数据绑定:可以使用TypeConverter而不是Format / Parse事件吗? [英] Winforms data binding: Can a TypeConverter be used instead of the Format/Parse events?
问题描述
ForeColor
属性绑定到(布尔) IsPropertyValid
属性底层模型使得当 IsPropertyValid == false
时,标签变红。 我目前拥有的是一个事件绑定的格式的处理程序
事件:
控件[dateOfBirthLabel] .DataBindings [ForeColor]。格式+ = convertBoolToColor;
//(dateOfBirthLabel.ForeColor绑定到一个布尔型IsDateOfBirthValid属性)
void convertBoolToColor(object sender,ConvertEventArgs e)
{
e.Value =( bool)e.Value? Color.Black:Color.Red;
}
如果我想在WPF中这样做,我想我会指定一个自定义价值转换器( bool
至颜色
)直接与XAML中的绑定。最重要的是,我不必通过其名称来引用具体的控件。
我想和我做同样的事情Winforms形式。理想情况下,我可以在Forms Designer中直接指定特定绑定的 TypeConverter
对象。这是可能吗?
我的以前的答案(现已删除)不正确:可以使用 .componentmodel.typeconverter.aspxrel =nofollowtitle =System.ComponentModel.TypeConverter的MSDN参考页面> TypeConverter
。
首先,需要一个合适的转换器。 (与XAML不同,没有实现 IValueConverter ,但是从 TypeConverter
)例如:
// using System;
// using System.ComponentModel;
//使用System.Drawing;
//使用System.Globalization;
密码类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)
{
返回(bool)值? Color.Green:Color.Red;
}
}
接下来也不同于XAML数据绑定,)该转换器不适用于绑定本身;它必须使用 [TypeConverter]
属性:
// using System.ComponentModel;
partial class DataSource:INotifyPropertyChanged
{
[TypeConverter(typeof(BooleanToColorConverter))] //< - 添加这个!
public bool IsValid {get {...} set {...}}
}
最后,的MSDN参考页面
//控制控件= ...;
// DataSource dataSource = ...;
control.DataBindings.Add(ForeColor,dataSource,IsValid,formattingEnabled:true);
// ^^^^^^^^^^^^^^^^^^^^^^
请注意,此示例仅涉及单向(数据源来控制)数据绑定。对于双向数据绑定,您还需要覆盖 TypeConverter.ConvertFrom
和 TypeConverter.CanConvertFrom
方法。 / p>
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 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.
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?
My previous answer (now deleted) was incorrect: This can be done, using a custom TypeConverter
.
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;
}
}
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);
// ^^^^^^^^^^^^^^^^^^^^^^^
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.ConvertFrom
and TypeConverter.CanConvertFrom
methods.
这篇关于Winforms数据绑定:可以使用TypeConverter而不是Format / Parse事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!