共用方式為


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

屬性

範例

下列範例是一個應用程式,會提示使用者輸入多個客戶,並將銷售代表指派給每個客戶,然後檢查銷售代表和客戶是否屬於相同的區域。 此範例會設定 ItemBindingGroupItemsControlValidationRule ,讓 、 AreasMatch 會驗證每個專案。 此範例也會建立 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 的 上呼叫 方法,而不是 ItemBindingGroup 在 的 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 ,其物件 ItemBindingGroup 與 相同 ValidationRule ,但描述系結中資料的屬性,例如 ItemsBindingExpressions ,都專屬於 中每個專案 ItemsControl 的資料。 您必須存取專案容器來執行 BindingGroup 作業,例如驗證資料,並檢查項目上的錯誤。

適用於