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

返回

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. 也就是说,如果源对象实现,则为 IEditableObjectThat 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.

适用于