ItemsControl.ItemBindingGroup Właściwość

Definicja

Pobiera lub ustawia BindingGroup wartość skopiowaną do każdego elementu w elemencie 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

Wartość właściwości

Element BindingGroup , który jest kopiowany do każdego elementu w elemencie ItemsControl.

Atrybuty

Przykłady

Poniższy przykład jest częścią aplikacji, która monituje użytkownika o wprowadzenie wielu klientów i przypisanie przedstawiciela sprzedaży do każdego klienta, a następnie sprawdza, czy przedstawiciel handlowy i klient należą do tego samego regionu. W przykładzie ustawiono element ItemBindingGroupItemsControl , aby ValidationRuleelement , AreasMatchzweryfikuje każdy element. W przykładzie utworzono również element Label , który wyświetla błędy walidacji. Zwróć uwagę, że właściwość LabelContent jest powiązana z obiektemValidationError, który pobiera z Validation.ValidationAdornerSiteForProperty właściwości . Wartość parametru Validation.ValidationAdornerSiteForProperty to kontener elementów, który zawiera błąd.

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

Poniższy przykład pobiera kontener elementów i wywołuje UpdateSources kontener kontenera BindingGroup w celu zweryfikowania danych. Należy zweryfikować dane, wywołując metodę w kontenerze BindingGroupelementów , a nie w ItemBindingGroupItemsControlobiekcie .

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

Uwagi

Po ustawieniu ItemBindingGroup właściwości każdy kontener elementów pobiera obiekt BindingGroup , który ma te same ValidationRule obiekty co ItemBindingGroupobiekt , ale właściwości opisujące dane w powiązaniach, takich jak Items i BindingExpressions, są specyficzne dla danych dla każdego elementu w ItemsControlobiekcie . Aby wykonać operacje, takie jak weryfikowanie danych i sprawdzanie błędów w elemencie, należy uzyskać dostęp do kontenera BindingGroup elementów.

Dotyczy