BindingGroup.GetValue(Object, String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定的属性和项的建议值。
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
要获取其建议值的属性。
返回
建议的属性值。
例外
指定项和属性之间没有绑定。
由于转换错误或早期验证规则失败,指定的属性值不可用。
示例
以下示例是提示用户输入多个客户并将销售代表分配给每个客户的应用程序的一部分。 应用程序会检查销售代表和客户是否属于同一区域。 该示例显示 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)已设置为RawProposedValue该ValidationStep属性,该方法将返回一个字符串。 ValidationRule如果设置了该方法ValidationStepConvertedProposedValue,该方法将返回绑定转换器返回的任何类型。 在此示例中, GetValue(Object, String) 通常返回整数。