ItemsControl.ItemBindingGroup Propriedade

Definição

Obtém ou define o BindingGroup que é copiado para cada item no 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; }
[<System.ComponentModel.Bindable(true)>]
member this.ItemBindingGroup : System.Windows.Data.BindingGroup with get, set
Public Property ItemBindingGroup As BindingGroup

Valor de Propriedade

O BindingGroup que é copiado para cada item no ItemsControl.

Atributos

Exemplos

O exemplo seguinte faz parte de uma aplicação que pede ao utilizador para introduzir vários clientes e atribuir um representante de vendas a cada cliente, verificando depois se o representante de vendas e o cliente pertencem à mesma região. O exemplo define o ItemBindingGroup de ItemsControl , de modo que o ValidationRule, AreasMatch, irá validar cada item. O exemplo também cria um Label que mostra erros de validação. Note que o Content de Label está ligado a um ValidationError que obtém da Validation.ValidationAdornerSiteForProperty propriedade. O valor de Validation.ValidationAdornerSiteForProperty é o recipiente do item que tem o erro.

<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"/>

O exemplo seguinte obtém o contentor do item e chama UpdateSources os contentores BindingGroup para validarem os dados. Deve validar os dados chamando um método no contentor BindingGroupdo item , e não no ItemBindingGroup do 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

Observações

Ao definir a ItemBindingGroup propriedade, cada contentor de itens recebe um BindingGroup que tem os mesmos ValidationRule objetos que o ItemBindingGroup, mas as propriedades que descrevem os dados nas ligações, como Items e BindingExpressions, são específicas para os dados de cada item no ItemsControl. Deve aceder aos contentores BindingGroup dos itens para realizar operações como validar os dados e verificar erros num item.

Aplica-se a