ConvertEventArgs.Value 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 的 ConvertEventArgs值。
public:
property System::Object ^ Value { System::Object ^ get(); void set(System::Object ^ value); };
public object Value { get; set; }
public object? Value { get; set; }
member this.Value : obj with get, set
Public Property Value As Object
屬性值
該 的價值 ConvertEventArgs。
範例
以下程式碼範例建立 ,為 ConvertEventHandler 和 事件都加入代理ParseFormat,並利用屬性DataBindings將 加入Binding控制項的 。BindingsCollectionTextBoxBinding
DecimalToCurrencyString事件Format代理(事件代理)會使用ToString該方法將綁定值(一種Decimal型別)格式化為貨幣。
CurrencyStringToDecimal事件Parse代理(event delegate)會將控制項顯示的值轉換回該Decimal類型。
private:
void DecimalToCurrencyString( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
// The method converts only to string type. Test this using the DesiredType.
if ( cevent->DesiredType != String::typeid )
{
return;
}
// Use the ToString method to format the value as currency ("c").
cevent->Value = ( (Decimal^)(cevent->Value) )->ToString( "c" );
}
void CurrencyStringToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
// The method converts back to decimal type only.
if ( cevent->DesiredType != Decimal::typeid )
{
return;
}
// Converts the string back to decimal using the static Parse method.
cevent->Value = Decimal::Parse( cevent->Value->ToString(),
NumberStyles::Currency, nullptr );
}
void BindControl()
{
// Creates the binding first. The OrderAmount is typed as Decimal.
Binding^ b = gcnew Binding(
"Text",ds,"customers.custToOrders.OrderAmount" );
// Adds the delegates to the events.
b->Format += gcnew ConvertEventHandler(
this, &Form1::DecimalToCurrencyString );
b->Parse += gcnew ConvertEventHandler(
this, &Form1::CurrencyStringToDecimal );
text1->DataBindings->Add( b );
}
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);
}
Private Sub DecimalToCurrencyString(sender As Object, cevent As ConvertEventArgs)
' The method converts only to string type. Test this using the DesiredType.
If Not cevent.DesiredType Is GetType(String) Then
Return
End If
' Use the ToString method to format the value as currency ("c").
cevent.Value = CDec(cevent.Value).ToString("c")
End Sub
Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
' The method converts back to decimal type only.
If Not cevent.DesiredType Is GetType(Decimal) Then
Return
End If
' Converts the string back to decimal using the shared Parse method.
cevent.Value = Decimal.Parse(cevent.Value.ToString, _
NumberStyles.Currency, nothing)
End Sub
Private Sub BindControl()
' Creates the binding first. The OrderAmount is typed as Decimal.
Dim b As New Binding("Text", ds, "customers.custToOrders.OrderAmount")
' Adds the delegates to the events.
AddHandler b.Format, AddressOf DecimalToCurrencyString
AddHandler b.Parse, AddressOf CurrencyStringToDecimal
text1.DataBindings.Add(b)
End Sub
備註
屬性所包含 Value 的價值取決於返回的 ConvertEventArgs 事件。 這些獎勵 ConvertEventArgs 可以在活動或 Format 活動中 Parse 退回。
當 在事件中回傳 Format 時ConvertEventArgs,該Value屬性包含資料來源未格式化的屬性值。 在事件中 Format ,你可以讀取屬性值、格式化值,並重設 Value 屬性為新的(格式化)值,從而設定資料綁定控制項中顯示的值。
當 在事件中Parse回傳 時ConvertEventArgs,屬性包含了資料綁定控制項的自訂格式值。 在事件中 Parse ,你必須讀取格式化的值,解析它,然後將其轉換回與資料來源相同的資料型別。 接著你可以將屬性重設 Value 為未格式化的值,從而設定資料來源的值。 要判斷資料來源類型,請檢視 DesiredType 房產價值。