BindingGroup.UpdateSources 方法

定義

對綁定 ValidationRule 及設定為 ValidationStepRawProposedValueConvertedProposedValue、 或 UpdatedValue 的物件執行轉換器,若所有驗證規則成功,則將目標值儲存至來源物件。

public:
 bool UpdateSources();
public bool UpdateSources();
member this.UpdateSources : unit -> bool
Public Function UpdateSources () As Boolean

傳回

true若所有驗證規則皆成功;否則,。 false

範例

以下範例是一個應用程式的一部分,該應用程式會提示使用者輸入多個客戶,並為每位客戶指派一位銷售代表。 申請會檢查銷售代表與顧客是否屬於同一地區。 範例中呼叫 UpdateSources 驗證綁定,若所有驗證規則成功,則將值儲存至來源。

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

備註

此方法若 Every ValidationRule 成功,會更新來源,但不會讓來源提交待處理變更並終止編輯交易。 也就是說,如果來源物件實 IEditableObject作了 。 呼叫此方法不會導致 EndEdit 被呼叫。 用這個 CommitEdit 方法讓來源提交待處理的變更。

適用於