ItemsControl.ItemBindingGroup Vlastnost

Definice

Získá nebo nastaví BindingGroup , který je zkopírován do každé položky v 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

Hodnota vlastnosti

Hodnota BindingGroup , která se zkopíruje do každé položky v objektu ItemsControl.

Atributy

Příklady

Následující příklad je součástí aplikace, která vyzve uživatele, aby zadal více zákazníků a každému zákazníkovi přiřadil obchodního zástupce, a pak zkontroluje, jestli obchodní zástupce a zákazník patří do stejné oblasti. V příkladu se ItemBindingGroup nastaví hodnota ItemsControl , takže ValidationRule, AreasMatchověří každou položku. Příklad také vytvoří chybu Label ověřování. Všimněte si, že Content hodnota je Label svázaná s objektem ValidationError , který získá z Validation.ValidationAdornerSiteForProperty vlastnosti. Hodnota Validation.ValidationAdornerSiteForProperty je kontejner položek, u kterého došlo k chybě.

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

Následující příklad získá kontejner položek a zavolá UpdateSources na kontejnery BindingGroup k ověření dat. Data je nutné ověřit voláním metody v kontejneru BindingGrouppoložky , nikoli v objektu ItemBindingGroupItemsControl.

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

Poznámky

Když nastavíte ItemBindingGroup vlastnost, každý kontejner položek získá BindingGroup objekt, který má stejné ValidationRule objekty jako ItemBindingGroup, ale vlastnosti, které popisují data ve vazbách, například Items a BindingExpressions, jsou specifické pro data pro každou položku v objektu ItemsControl. Abyste mohli provádět operace, jako je ověření dat a kontrola chyb u položky, musíte přistupovat k kontejnerům BindingGroup položek.

Platí pro