ItemsControl.ItemBindingGroup ItemsControl.ItemBindingGroup ItemsControl.ItemBindingGroup ItemsControl.ItemBindingGroup Property

定義

BindingGroup の各項目にコピーされる ItemsControl を取得または設定します。Gets or sets the BindingGroup that is copied to each item in the 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; }
member this.ItemBindingGroup : System.Windows.Data.BindingGroup with get, set
Public Property ItemBindingGroup As BindingGroup

プロパティ値

BindingGroup の各項目にコピーされる ItemsControlThe BindingGroup that is copied to each item in the ItemsControl.

次の例では、複数の顧客を入力し、顧客ごとに、営業担当者の割り当てをユーザーに求める、アプリケーションの一部であり、営業担当者と、顧客が同じリージョンに属していることを確認します。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. 例のセット、ItemBindingGroupItemsControlため、 ValidationRuleAreasMatch、各項目を検証します。The example sets the ItemBindingGroup of the ItemsControl so the ValidationRule, AreasMatch, will validate each item. 例でも作成、Label検証エラーを表示します。The example also creates a Label that displays validation errors. 注意、ContentLabelにバインドされて、ValidationErrorから取得した、Validation.ValidationAdornerSiteForPropertyプロパティ。Notice that the Content of the Label is bound to a ValidationError that it gets from the Validation.ValidationAdornerSiteForProperty property. Validation.ValidationAdornerSiteForPropertyは、エラーのある項目のコンテナーです。The value of Validation.ValidationAdornerSiteForProperty is the item container that has the 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"/>

次の例では、項目コンテナーと呼び出しを取得します。UpdateSourcesでコンテナーのBindingGroup、データを検証します。The following example gets the item container and calls UpdateSources on the container's BindingGroup to validate the data. 項目コンテナーのメソッドを呼び出すことによって、データを検証する必要がありますBindingGroupのない、ItemBindingGroupItemsControlします。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;
    }

}
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

注釈

設定すると、ItemBindingGroupプロパティ、各項目のコンテナーを取得、BindingGroupを持つ同じValidationRuleとしてオブジェクト、ItemBindingGroupなど、バインドでデータを記述するプロパティが、ItemsBindingExpressionsは内の各項目のデータを特定、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. 項目コンテナーのアクセスする必要がありますBindingGroupを実行する操作検証など、データと項目のエラーを確認します。You must access the item container's BindingGroup to perform operations such as validate the data and check for errors on an item.

適用対象