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。 调用此方法不会导致 EndEdit 调用。 使用该方法 CommitEdit 让源提交挂起的更改。

适用于