ConvertEventArgs.DesiredType 属性

定义

获取所需值的数据类型。

public:
 property Type ^ DesiredType { Type ^ get(); };
public Type DesiredType { get; }
public Type? DesiredType { get; }
member this.DesiredType : Type
Public ReadOnly Property DesiredType As Type

属性值

Type

所需值的 Type

示例

下面的代码示例使用 DesiredType 该属性来确定一种类型的转换是否可以继续。 该方法 DecimalToCurrencyString 测试该 DesiredType 字符串是否为字符串。 否则,代码将退出该方法。 同样,该方法 CurrencyStringToDecimal 测试该 DesiredType 对象是否为一个 Decimal,如果不是 true,则退出。

private:
   void DecimalToCurrencyString( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts only to string type. 
      if ( cevent->DesiredType != String::typeid )
      {
         return;
      }

      cevent->Value = ( (Decimal^)(cevent->Value) )->ToString( "c" );
   }

   void CurrencyStringToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts only to decimal type.
      if ( cevent->DesiredType != Decimal::typeid )
      {
         return;
      }

      cevent->Value = Decimal::Parse( cevent->Value->ToString(),
         NumberStyles::Currency, nullptr );
   }
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. 
   if(cevent.DesiredType != typeof(string)) return;

   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

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

   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}
Private Sub DecimalToCurrencyString(sender As Object, cevent As ConvertEventArgs)
   ' The method converts only to string type. 
   If cevent.DesiredType IsNot GetType(String) Then
      Return
   End If 
   cevent.Value = CDec(cevent.Value).ToString("c")
End Sub 
 
 
Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
   ' The method converts only to decimal type.
   If cevent.DesiredType IsNot GetType(Decimal) Then
      Return
   End If 
 cevent.Value = Decimal.Parse(cevent.Value.ToString, _
 NumberStyles.Currency, nothing)
End Sub

注解

使用该 DesiredType 属性可以检查要转换为的值的属性的类型。

适用于