BindingGroup.Items 屬性

定義

取得 BindingGroup 中之 Binding 物件所使用的來源。

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

屬性值

IList

BindingGroup 中之 Binding 物件所使用的來源。

範例

下列範例是應用程式一部分,會檢查使用者是否已將兩個物件的屬性設定為相等值。 第一個範例會建立兩 TextBox 個控制項,每個控制項都是系結至不同來源的資料。 第一個 TextBox 的系結會從 DataContext TextBox 控制項的父元素 () 取得 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 只有一個專案,也就是 使用 BindingGroup 之專案的 物件 DataContextBindingGroup不過,可以有多個來源。 例如,如果 Binding 物件共用相同 BindingGroupName 但使用不同的來源物件,則做為來源的每個物件都位於 Items 中。

如果 Binding 的路徑解析為來源的巢狀屬性,也可能有多個 Items 物件。 例如,假設 TextBox 控制項的系結是 的 BindingGroup 一部分,而它是 物件,其 DataContext 具有 Customer 類型的 Address 屬性。 Path如果 的 BindingAddress.ZipCode 屬性,則會 Address 將 新增至 Items 屬性。

適用於