ItemsControl.ItemBindingGroup Propiedad

Definición

Obtiene o establece el BindingGroup que se copia en cada elemento en el 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 propiedad

BindingGroup que se copia en cada elemento en el ItemsControl.

Atributos

Ejemplos

El ejemplo siguiente forma parte de una aplicación que solicita al usuario que escriba varios clientes y asigne un representante de ventas a cada cliente y, a continuación, compruebe que el representante de ventas y el cliente pertenecen a la misma región. En el ejemplo se establece el ItemBindingGroup de ItemsControl para que ValidationRule, AreasMatch, validará cada elemento. En el ejemplo también se crea un Label objeto que muestra los errores de validación. Observe que el Content de Label está enlazado a un ValidationError objeto que obtiene de la Validation.ValidationAdornerSiteForProperty propiedad . El valor de Validation.ValidationAdornerSiteForProperty es el contenedor de elementos que tiene el 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"/>

En el ejemplo siguiente se obtiene el contenedor de elementos y se llama UpdateSources a en el contenedor BindingGroup para validar los datos. Debe validar los datos llamando a un método en el contenedor de BindingGroupelementos , no en el ItemBindingGroup de 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

Comentarios

Al establecer la ItemBindingGroup propiedad , cada contenedor de elementos obtiene un BindingGroup objeto que tiene los mismos ValidationRule objetos que , ItemBindingGrouppero las propiedades que describen los datos de los enlaces, como Items y BindingExpressions, son específicos de los datos de cada elemento de ItemsControl. Debe tener acceso al contenedor de BindingGroup elementos para realizar operaciones como validar los datos y comprobar si hay errores en un elemento.

Se aplica a