BindingBase.BindingGroupName 属性

定义

获取或设置此绑定所属的 BindingGroup 的名称。

public:
 property System::String ^ BindingGroupName { System::String ^ get(); void set(System::String ^ value); };
public string BindingGroupName { get; set; }
member this.BindingGroupName : string with get, set
Public Property BindingGroupName As String

属性值

String

此绑定所属的 BindingGroup 的名称。

示例

以下示例是应用程序一部分,用于检查用户是否已将两个对象的属性设置为相等值。 第一个示例创建两TextBox个控件,其中每个控件都是绑定到不同源的数据:第一TextBox个控件的绑定是继承其父级的一部分BindingGroup TextBox DataContext BindingGroupStackPanel

第二TextBox个绑定是其中的一部分BindingGroup,因为Name BindingGroupName BindingGroup Binding两者都设置为 。bindingGroup

<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

注解

如果未 BindingGroupName 设置,则绑定仅当 BindingGroup 绑定的源和 DataContext 具有 BindingGroup 相同对象的父元素时才属于该绑定。

如果 BindingGroupName 设置为值,则绑定属于 BindingGroup 以下条件为 true:

如果 BindingGroupName 设置为 null,则绑定绝不是其中的一 BindingGroup部分。

可以包含具有与父元素不同的DataContext源的绑定,该元素具有绑定到相同值的绑定NameBindingGroupBindingGroupNameBindingGroup属性。 可以排除具有与DataContext父元素相同的源的绑定,该元素BindingGroup具有绑定到不同值的绑定NameBindingGroupBindingGroupName属性。

适用于