Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 4.62 KB

how-to-control-when-the-textbox-text-updates-the-source.md

File metadata and controls

47 lines (32 loc) · 4.62 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Control When the TextBox Text Updates the Source
Control the timing of binding source updates using the UpdateSourceTrigger property in Windows Presentation Foundation (WPF).
03/30/2017
csharp
vb
source updates [WPF], timing of
data binding [WPF], timing of source updates
timing of source updates [WPF]
ffb7b96a-351d-4c68-81e7-054033781c64

How to: Control When the TextBox Text Updates the Source

This topic describes how to use the xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A property to control the timing of binding source updates. The topic uses the xref:System.Windows.Controls.TextBox control as an example.

Example

The xref:System.Windows.Controls.TextBox.Text%2A?displayProperty=nameWithType property has a default xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A value of xref:System.Windows.Data.UpdateSourceTrigger.LostFocus. This means if an application has a xref:System.Windows.Controls.TextBox with a data-bound xref:System.Windows.Controls.TextBox.Text%2A?displayProperty=nameWithType property, the text you type into the xref:System.Windows.Controls.TextBox does not update the source until the xref:System.Windows.Controls.TextBox loses focus (for instance, when you click away from the xref:System.Windows.Controls.TextBox).

If you want the source to be updated as you type, set the xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A of the binding to xref:System.Windows.Data.UpdateSourceTrigger.PropertyChanged. In the following example, the highlighted lines of code show that the Text properties of both the xref:System.Windows.Controls.TextBox and the xref:System.Windows.Controls.TextBlock are bound to the same source property. The xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A property of the xref:System.Windows.Controls.TextBox binding is set to xref:System.Windows.Data.UpdateSourceTrigger.PropertyChanged.

[!code-xamlSimpleBinding#USTHowTo]

As a result, the xref:System.Windows.Controls.TextBlock shows the same text (because the source changes) as the user enters text into the xref:System.Windows.Controls.TextBox, as illustrated by the following screenshot of the sample:

Screenshot that shows simple data binding.

If you have a dialog or a user-editable form and you want to defer source updates until the user is finished editing the fields and clicks "OK", you can set the xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A value of your bindings to xref:System.Windows.Data.UpdateSourceTrigger.Explicit, as in the following example:

[!code-xamlUpdateSource#2]

When you set the xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A value to xref:System.Windows.Data.UpdateSourceTrigger.Explicit, the source value only changes when the application calls the xref:System.Windows.Data.BindingExpression.UpdateSource%2A method. The following example shows how to call xref:System.Windows.Data.BindingExpression.UpdateSource%2A for itemNameTextBox:

[!code-csharpUpdateSource#1] [!code-vbUpdateSource#1]

Note

You can use the same technique for properties of other controls, but keep in mind that most other properties have a default xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A value of xref:System.Windows.Data.UpdateSourceTrigger.PropertyChanged. For more information, see the xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A property page.

Note

The xref:System.Windows.Data.Binding.UpdateSourceTrigger%2A property deals with source updates and therefore is only relevant for xref:System.Windows.Data.BindingMode.TwoWay or xref:System.Windows.Data.BindingMode.OneWayToSource bindings. For xref:System.Windows.Data.BindingMode.TwoWay and xref:System.Windows.Data.BindingMode.OneWayToSource bindings to work, the source object needs to provide property change notifications. You can refer to the samples cited in this topic for more information. In addition, you can look at Implement Property Change Notification.

See also