ItemsControl.ItemBindingGroup 属性

定义

获取或设置复制到 BindingGroup 中每个项的 ItemsControlGets or sets the BindingGroup that is copied to each item in the ItemsControl.

public:
 property System::Windows::Data::BindingGroup ^ ItemBindingGroup { System::Windows::Data::BindingGroup ^ get(); void set(System::Windows::Data::BindingGroup ^ value); };
[System.ComponentModel.Bindable(true)]
public System.Windows.Data.BindingGroup ItemBindingGroup { get; set; }
member this.ItemBindingGroup : System.Windows.Data.BindingGroup with get, set
Public Property ItemBindingGroup As BindingGroup

属性值

复制到 BindingGroup 中每个项的 ItemsControlThe BindingGroup that is copied to each item in the ItemsControl.

属性

示例

下面的示例是一个应用程序的一部分,该应用程序提示用户输入多个客户,并为每个客户分配一个销售代表,然后检查销售代表和客户是否属于同一区域。The following example is part of an application that prompts the user to enter multiple customers and assign a sales representative to each customer, and then checks that the sales representative and the customer belong to the same region. 该示例设置了 ItemsControlItemBindingGroup,因此 ValidationRuleAreasMatch将验证每个项。The example sets the ItemBindingGroup of the ItemsControl so the ValidationRule, AreasMatch, will validate each item. 该示例还创建了一个显示验证错误的 LabelThe example also creates a Label that displays validation errors. 请注意,LabelContent 绑定到从 Validation.ValidationAdornerSiteForProperty 属性获取的 ValidationErrorNotice that the Content of the Label is bound to a ValidationError that it gets from the Validation.ValidationAdornerSiteForProperty property. Validation.ValidationAdornerSiteForProperty 的值是包含错误的项容器。The value of Validation.ValidationAdornerSiteForProperty is the item container that has the error.

<ItemsControl Name="customerList"  ItemTemplate="{StaticResource ItemTemplate}"
              ItemsSource="{Binding}">
  <ItemsControl.ItemBindingGroup>
    <BindingGroup>
      <BindingGroup.ValidationRules>
        <src:AreasMatch/>
      </BindingGroup.ValidationRules>
    </BindingGroup>
  </ItemsControl.ItemBindingGroup>
  <ItemsControl.ItemContainerStyle>
    <Style TargetType="{x:Type ContentPresenter}">
      <Setter Property="Validation.ValidationAdornerSite"
              Value="{Binding ElementName=validationErrorReport}"/>
    </Style>
  </ItemsControl.ItemContainerStyle>
</ItemsControl>
<Label Name="validationErrorReport" 
       Content="{Binding RelativeSource={RelativeSource Self}, 
       Path=(Validation.ValidationAdornerSiteFor).(Validation.Errors)[0].ErrorContent}"
       Margin="5" Foreground="Red" HorizontalAlignment="Center"/>

下面的示例获取项容器,并调用容器的 BindingGroup UpdateSources 来验证数据。The following example gets the item container and calls UpdateSources on the container's BindingGroup to validate the data. 必须通过对项容器的 BindingGroup调用方法,而不是在 ItemsControlItemBindingGroup 上调用方法来验证数据。You must validate the data by calling a method on the item container's BindingGroup, not on the ItemBindingGroup of the ItemsControl.

void saveCustomer_Click(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    FrameworkElement container = (FrameworkElement) customerList.ContainerFromElement(btn);

    // If the user is trying to change an items, when another item has an error,
    // display a message and cancel the currently edited item.
    if (bindingGroupInError != null && bindingGroupInError != container.BindingGroup)
    {
        MessageBox.Show("Please correct the data in error before changing another customer");
        container.BindingGroup.CancelEdit();
        return;
    }

    if (container.BindingGroup.UpdateSources())
    {
        bindingGroupInError = null;
        MessageBox.Show("Item Saved");
    }
    else
    {
        bindingGroupInError = container.BindingGroup;
    }
}
Private Sub saveCustomer_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim btn As Button = TryCast(sender, Button)
    Dim container As FrameworkElement = CType(customerList.ContainerFromElement(btn), FrameworkElement)

    ' If the user is trying to change an items, when another item has an error,
    ' display a message and cancel the currently edited item.
    If bindingGroupInError IsNot Nothing AndAlso bindingGroupInError IsNot container.BindingGroup Then
        MessageBox.Show("Please correct the data in error before changing another customer")
        container.BindingGroup.CancelEdit()
        Return
    End If

    If container.BindingGroup.UpdateSources() Then
        bindingGroupInError = Nothing
        MessageBox.Show("Item Saved")
    Else
        bindingGroupInError = container.BindingGroup
    End If

End Sub

注解

设置 ItemBindingGroup 属性时,每个项容器均获取与 ItemBindingGroup具有相同 ValidationRule 对象的 BindingGroup,但描述绑定中的数据的属性(如 ItemsBindingExpressions)特定于 ItemsControl中每个项的数据。When you set the ItemBindingGroup property, each item container gets a BindingGroup that has the same ValidationRule objects as the ItemBindingGroup, but the properties that describe the data in the bindings, such as Items and BindingExpressions, are specific to the data for each item in the ItemsControl. 您必须访问项容器的 BindingGroup 以执行操作,例如验证数据和检查项是否有错误。You must access the item container's BindingGroup to perform operations such as validate the data and check for errors on an item.

适用于