ItemsControl.ItemBindingGroup プロパティ

定義

ItemsControl の各項目にコピーされる BindingGroup を取得または設定します。

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

プロパティ値

ItemsControl の各項目にコピーされる BindingGroup

属性

次の例は、複数の顧客を入力し、各顧客に営業担当者を割り当て、営業担当者と顧客が同じリージョンに属していることを確認するようにユーザーに求めるアプリケーションの一部です。 この例では、 の がItemBindingGroupItemsControl各項目をValidationRuleAreasMatch検証するように の を設定します。 この例では、検証エラーを表示する も作成 Label します。 ContentLabel は、 プロパティから取得した にValidationErrorバインドされていることにValidation.ValidationAdornerSiteForProperty注意してください。 の Validation.ValidationAdornerSiteForProperty 値は、エラーが発生した項目コンテナーです。

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

次の例では、項目コンテナーを取得し、コンテナーBindingGroupの を呼び出UpdateSourcesしてデータを検証します。 の ではなく、項目コンテナーの BindingGroupの メソッドを呼び出して、データを検証する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

注釈

プロパティをItemBindingGroup設定すると、各項目コンテナーは と同じValidationRuleオブジェクトItemBindingGroupを持つ を取得BindingGroupしますが、 や BindingExpressionsなどのItemsバインディング内のデータを記述するプロパティは、 内ItemsControlの各アイテムのデータに固有です。 アイテムコンテナーにアクセスして、データのBindingGroup検証やアイテムのエラーのチェックなどの操作を実行する必要があります。

適用対象