使用英语阅读

通过


ConvertEventArgs.Value 属性

定义

重要

一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。

获取或设置 ConvertEventArgs 的值。

C#
public object Value { get; set; }
C#
public object? Value { get; set; }

属性值

ConvertEventArgs 的值。

示例

下面的代码示例创建 一个 Binding,将委托ConvertEventHandler添加到 ParseFormat 事件,并使用 DataBindings 属性将 BindingsCollection 添加到Binding控件的 TextBoxDecimalToCurrencyString添加到 Format 事件的事件委托使用 ToString 方法将绑定值格式化 (Decimal类型) 为货币。 CurrencyStringToDecimal添加到 事件的事件Parse委托将控件显示的值转换回 类型Decimal

C#
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. Test this using the DesiredType.
   if(cevent.DesiredType != typeof(string)) return;

   // Use the ToString method to format the value as currency ("c").
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
   // The method converts back to decimal type only. 
   if(cevent.DesiredType != typeof(decimal)) return;

   // Converts the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

private void BindControl()
{
   // Creates the binding first. The OrderAmount is typed as Decimal.
   Binding b = new Binding
   ("Text", ds, "customers.custToOrders.OrderAmount");
   // Adds the delegates to the events.
   b.Format += new ConvertEventHandler(DecimalToCurrencyString);
   b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

注解

属性包含 Value 的值取决于返回 的事件 ConvertEventArgsConvertEventArgs可以在 事件或 Parse 事件中Format返回 。

ConvertEventArgs在 事件中Format返回 时,Value属性包含数据源的未格式化属性值。 在 事件中 Format ,可以读取属性值、设置值格式,并将属性重置 Value 为新 (格式化) 值,从而设置数据绑定控件中显示的值。

ConvertEventArgs在 事件中Parse返回 时, 属性包含数据绑定控件的自定义格式值。 在 事件中 Parse ,必须读取格式化值,对其进行分析,然后将其转换回与数据源相同的数据类型。 然后, Value 可以将 属性重置为未格式化的值,从而设置数据源的值。 若要确定数据源的类型,请检查 DesiredType 属性值。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅