共用方式為


BindingGroup.UpdateSources 方法

定義

如果所有驗證規則都成功,則會對此繫結和屬性 ValidationRule 設定為 ValidationStepRawProposedValueConvertedProposedValueUpdatedValue 物件執行該轉換子,並將目標值儲存至來源物件。

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

傳回

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

備註

如果每次 ValidationRule 成功,這個方法就會更新來源,但不會讓來源認可擱置的變更,並結束編輯交易。 也就是說,如果來源物件實作 ,則為 IEditableObject 。 呼叫這個方法並不會呼叫 EndEditCommitEdit使用 方法讓來源認可擱置的變更。

適用於