BindingGroup.GetValue(Object, String) 方法

定義

傳回指定之屬性與項目的建議值。Returns the proposed value for the specified property and item.

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

包含指定之屬性的物件。The object that contains the specified property.

propertyName
String

要取得其建議值的屬性。The property whose proposed value to get.

傳回

建議的屬性值。The proposed property value.

例外狀況

指定之項目和屬性沒有繫結。There is not a binding for the specified item and property.

無法取得指定之屬性的值,因為發生轉換錯誤,或先前的驗證規則失敗。The value of the specified property is not available, due to a conversion error or because an earlier validation rule failed.

範例

下列範例是應用程式的一部分,它會提示使用者輸入多個客戶,並將銷售代表指派給每個客戶。The following example is part of an application that prompts the user to enter multiple customers and assign a sales representative to each customer. 應用程式會檢查「銷售代表」和「客戶」是否屬於相同的區域。The application checks that the sales representative and the customer belong to the same region. 此範例Validate示範方法,其GetValue(Object, String)使用方法來取得客戶輸入的值。The example shows the Validate method, which uses the GetValue(Object, String) method to get values that the customer entered.

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方法中使用這個方法,以取得要認可至來源的值。Use this method in the ValidationRule.Validate method to get the value to be committed to the source. 傳回值的類型取決於發生的ValidationRule階段。The type of the return value depends on the stage at which the ValidationRule occurs. 例如TextBox ,如果是系結至整數類型之屬性的資料GetValue(Object, String) ValidationRule ,而呼叫的已將其ValidationStep設定為RawProposedValue,則方法會傳回字串。For example, if a TextBox is data bound to a property of type integer and the ValidationRule that calls GetValue(Object, String) has its ValidationStep set to RawProposedValue, the method returns a string. 如果將ValidationRule ValidationStep設為ConvertedProposedValue,則方法會傳回系結的轉換器所傳回的任何型別。If the ValidationRule has its ValidationStep set to ConvertedProposedValue, the method returns whatever type that is returned by the binding's converter. 在此範例中GetValue(Object, String) ,通常會傳回整數。In this example, GetValue(Object, String) usually returns an integer.

適用於