ItemsControl.ItemBindingGroup Propiedad

Definición

Obtiene o establece el BindingGroup que se copia en cada elemento en el ItemsControl.

C#
[System.ComponentModel.Bindable(true)]
public System.Windows.Data.BindingGroup ItemBindingGroup { get; set; }

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.

XAML
<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>
XAML
<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.

C#
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;
    }
}

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

Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10