BindingGroup.UpdateSources 方法

定義

如果所有驗證規則都成功,則會對此繫結和屬性 ValidationRule 設定為 ValidationStepRawProposedValueConvertedProposedValueUpdatedValue 物件執行該轉換子,並將目標值儲存至來源物件。Runs the converter on the binding and the ValidationRule objects that have the ValidationStep property set to RawProposedValue, ConvertedProposedValue, or UpdatedValue and saves the values of the targets to the source objects if all the validation rules succeed.

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

傳回

如果所有驗證規則成功,則為 true,否則為 falsetrue if all validation rules succeed; otherwise, false.

範例

下列範例是應用程式的一部分,它會提示使用者輸入多個客戶,並將銷售代表指派給每個客戶。The following example is part of an application that prompts the user to enter multiple customers and assign a sales representative to each customer. 應用程式會檢查「銷售代表」和「客戶」是否屬於相同的區域。The application checks that the sales representative and the customer belong to the same region. 範例會呼叫UpdateSources來驗證系結,並在所有驗證規則都成功時將值儲存至來源。The example calls UpdateSources to validate the bindings and save the values to the source if all the validation rules succeed.

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

備註

如果每次ValidationRule成功,這個方法會更新來源,但不會造成來源認可暫止的變更,並結束編輯交易。This method updates the source if every ValidationRule succeeds, but it does not cause the sources to commit the pending changes and end the edit transaction. 也就是,如果來源物件IEditableObject已執行,則為。That is, if the source object implements IEditableObject. 呼叫這個方法並不會EndEdit造成呼叫。Calling this method does not cause EndEdit to be called. CommitEdit使用方法,讓來源認可暫止的變更。Use the CommitEdit method to have the sources commit the pending changes.

適用於