ItemsControl.ItemBindingGroup 属性

定义

获取或设置复制到 ItemsControl 中每个项的 BindingGroup

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; }
[<System.ComponentModel.Bindable(true)>]
member this.ItemBindingGroup : System.Windows.Data.BindingGroup with get, set
Public Property ItemBindingGroup As BindingGroup

属性值

复制到 ItemsControl 中每个项的 BindingGroup

属性

示例

以下示例是应用程序的一部分,该应用程序提示用户输入多个客户并为每个客户分配一个销售代表,然后检查销售代表和客户是否属于同一区域。 该示例设置 ItemBindingGroup 的 ,ItemsControl因此 ValidationRuleAreasMatch, 将验证每个项。 该示例还创建一个 Label 显示验证错误的 。 请注意, ContentLabel 绑定到 ValidationError 它从 属性获取的 Validation.ValidationAdornerSiteForProperty 。 的 值为 Validation.ValidationAdornerSiteForProperty 具有错误的项容器。

<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 以验证数据。 必须通过在项容器的 BindingGroup上调用 方法来验证数据,而不是 在 的 ItemsControlItemBindingGroup调用 方法。

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 属性时,每个项容器都将获取一个 BindingGroup ,该容器具有与 相同的 ValidationRule 对象 ItemBindingGroup,但描述绑定中数据的属性(如 ItemsBindingExpressions)特定于 中 ItemsControl每个项的数据。 必须访问项容器才能BindingGroup执行验证数据和检查项错误等操作。

适用于