Share via


BindingGroup.GetValue(Object, String) 方法

定義

傳回指定之屬性與項目的建議值。

public:
 System::Object ^ GetValue(System::Object ^ item, System::String ^ propertyName);
public object GetValue (object item, string propertyName);
override this.GetValue : obj * string -> obj
Public Function GetValue (item As Object, propertyName As String) As Object

參數

item
Object

包含指定之屬性的物件。

propertyName
String

要取得其建議值的屬性。

傳回

Object

建議的屬性值。

例外狀況

指定之項目和屬性沒有繫結。

無法取得指定之屬性的值,因為發生轉換錯誤,或先前的驗證規則失敗。

範例

下列範例是應用程式一部分,會提示使用者輸入多個客戶,並將銷售代表指派給每位客戶。 應用程式會檢查銷售代表和客戶是否屬於相同的區域。 此範例顯示 Validate 方法,此方法會使用 GetValue(Object, String) 方法來取得客戶輸入的值。

public class AreasMatch : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        BindingGroup bg = value as BindingGroup;
        Customer cust = bg.Items[0] as Customer;
        
        if (cust == null)
        {
            return new ValidationResult(false, "Customer is not the source object");
        }

        Region region = (Region)bg.GetValue(cust, "Location");
        ServiceRep rep = bg.GetValue(cust, "ServiceRepresentative") as ServiceRep;
        string customerName = bg.GetValue(cust, "Name") as string;

        if (region == rep.Area)
        {
            return ValidationResult.ValidResult;
        }
        else
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. \n ", customerName, region);
            return new ValidationResult(false, sb.ToString());
        }
    }
}
Public Class AreasMatch
    Inherits ValidationRule
    Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As ValidationResult
        Dim bg As BindingGroup = TryCast(value, BindingGroup)
        Dim cust As Customer = TryCast(bg.Items(0), Customer)

        If cust Is Nothing Then
            Return New ValidationResult(False, "Customer is not the source object")
        End If

        Dim region As Region = CType(bg.GetValue(cust, "Location"), Region)
        Dim rep As ServiceRep = TryCast(bg.GetValue(cust, "ServiceRepresentative"), ServiceRep)
        Dim customerName As String = TryCast(bg.GetValue(cust, "Name"), String)

        If region = rep.Area Then
            Return ValidationResult.ValidResult
        Else

            Dim sb As New StringBuilder()
            sb.AppendFormat("{0} must be assigned a sales representative that serves the {1} region. " & vbLf & " ", customerName, region)
            Return New ValidationResult(False, sb.ToString())
        End If
    End Function
End Class

備註

在 方法中使用 ValidationRule.Validate 此方法,以取得要認可至來源的值。 傳回值的型別取決於 發生的階段 ValidationRule 。 例如,如果 TextBox 是系結至整數型別屬性的資料,且 ValidationRule 呼叫 GetValue(Object, String)ValidationStep RawProposedValue 設定為 ,則方法會傳回字串。 ValidationRule如果 已將其 ValidationStep 設定為 ConvertedProposedValue ,則 方法會傳回系結轉換子所傳回的任何類型。 在此範例中, GetValue(Object, String) 通常會傳回整數。

適用於