BindingGroup.Items 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 BindingGroup 中的绑定对象所使用的源。
public:
property System::Collections::IList ^ Items { System::Collections::IList ^ get(); };
public System.Collections.IList Items { get; }
member this.Items : System.Collections.IList
Public ReadOnly Property Items As IList
属性值
BindingGroup 中的绑定对象所使用的源。
示例
以下示例是应用程序一部分,用于检查用户是否已将两个对象的属性设置为相等值。 第一个示例创建两 TextBox 个控件,每个控件都是绑定到不同源的数据。 第一TextBox个绑定从DataContextTextBox控件的父元素 (StackPanel) 获取其源object1
。 第二 TextBox个,绑定的源设置为 object2
。 该示例还创建一个 Label 显示验证错误的实例。
<StackPanel Name="sp1"
Margin="5"
DataContext="{Binding Source={StaticResource object1}}"
Validation.ValidationAdornerSite="{Binding ElementName=label1}"
Orientation="Horizontal"
HorizontalAlignment="Center">
<StackPanel.BindingGroup>
<BindingGroup Name="bindingGroup">
<BindingGroup.ValidationRules>
<src:BindingGroupValidationRule ValidatesOnTargetUpdated="True" />
</BindingGroup.ValidationRules>
</BindingGroup>
</StackPanel.BindingGroup>
<TextBlock Text="First string" />
<TextBox Width="150"
Text="{Binding Path=PropertyA}" />
<TextBlock Text="Second string" />
<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue=please enter a string}" />
</StackPanel>
<Label Name="label1"
Content="{Binding ElementName=sp1, Path=(Validation.Errors)[0].ErrorContent}"
Margin="5"
Foreground="Red"
HorizontalAlignment="Center" />
下面的示例演示 ValidationRule 了上一个示例使用的示例。 在方法中 Validate ,该示例从 BindingGroup 中获取每个源对象,并检查对象的属性是否相等。
public class Type1
{
public string PropertyA { get; set; }
public Type1()
{
PropertyA = "Default Value";
}
}
public class Type2
{
public string PropertyB { get; set; }
public Type2()
{
}
}
public class BindingGroupValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
BindingGroup bg = value as BindingGroup;
Type1 object1 = null;
Type2 object2 = null;
foreach (object item in bg.Items)
{
if (item is Type1)
{
object1 = item as Type1;
}
if (item is Type2)
{
object2 = item as Type2;
}
}
if (object1 == null || object2 == null)
{
return new ValidationResult(false, "BindingGroup did not find source object.");
}
string string1 = bg.GetValue(object1, "PropertyA") as string;
string string2 = bg.GetValue(object2, "PropertyB") as string;
if (string1 != string2)
{
return new ValidationResult(false, "The two strings must be identical.");
}
return ValidationResult.ValidResult;
}
}
Public Class Type1
Public Property PropertyA() As String
Public Sub New()
PropertyA = "Default Value"
End Sub
End Class
Public Class Type2
Public Property PropertyB() As String
Public Sub New()
End Sub
End Class
Public Class BindingGroupValidationRule
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 object1 As Type1 = Nothing
Dim object2 As Type2 = Nothing
For Each item As Object In bg.Items
If TypeOf item Is Type1 Then
object1 = TryCast(item, Type1)
End If
If TypeOf item Is Type2 Then
object2 = TryCast(item, Type2)
End If
Next item
If object1 Is Nothing OrElse object2 Is Nothing Then
Return New ValidationResult(False, "BindingGroup did not find source object.")
End If
Dim string1 As String = TryCast(bg.GetValue(object1, "PropertyA"), String)
Dim string2 As String = TryCast(bg.GetValue(object2, "PropertyB"), String)
If string1 <> string2 Then
Return New ValidationResult(False, "The two strings must be identical.")
End If
Return ValidationResult.ValidResult
End Function
End Class
注解
用作源的每个对象都会添加到 Items 属性中一次,即使该对象用作多个绑定的源也是如此。 通常,只有一个项 Items,它是 DataContext 使用该 BindingGroup元素的对象。 BindingGroup但是,可以有多个源。 例如,如果 Binding 对象共享相同BindingGroupName但使用不同的源对象,则用作源的每个对象都在 。Items
如果绑定的路径解析为源的嵌套属性,则还可以存在多个对象 Items 。 例如,假设TextBox控件的绑定是控件BindingGroup的一部分,它是一个Customer
对象,该DataContext对象具有类型的Address
属性。 Path如果属性为Address.ZipCode
Binding该属性,则会Address
将该属性添加到该Items属性。