的WinForms数据绑定:可以一个类型转换器来代替格式/解析事件? [英] Winforms data binding: Can a TypeConverter be used instead of the Format/Parse events?

查看:171
本文介绍了的WinForms数据绑定:可以一个类型转换器来代替格式/解析事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个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.

这篇关于的WinForms数据绑定:可以一个类型转换器来代替格式/解析事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆