ItemsControl.ItemBindingGroup Property

Updated: July 2008

Gets or sets the BindingGroup that is copied to each item in the ItemsControl.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public function get ItemBindingGroup () : BindingGroup 
public function set ItemBindingGroup (value : BindingGroup)
<object>
  <object.ItemBindingGroup>
    <BindingGroup .../>
  </object.ItemBindingGroup>
</object>

Property Value

Type: System.Windows.Data.BindingGroup
The BindingGroup that is copied to each item in the 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. You must access the item container's BindingGroup to perform operations such as validate the data and check for errors on an item.

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. The example sets the ItemBindingGroup of the ItemsControl so the ValidationRule, AreasMatch, will validate each item. The example also creates a Label that displays validation errors. Notice that the Content of the Label is bound to a ValidationError that it gets from the Validation.ValidationAdornerSiteFor property. The value of Validation.ValidationAdornerSiteFor is the item container that has the error. For the entire example, see Validate an Item in an ItemsControl Sample.

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

The following example gets the item container and calls UpdateSources on the container's BindingGroup to validate the data. 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;
    }

}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2

Date

History

Reason

July 2008

Added topic for new member.

SP1 feature change.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft