匯出 (0) 列印
全部展開

Binding.UpdateSourceTrigger 屬性

Gets or sets a value that determines the timing of binding source updates.

命名空間: System.Windows.Data
組件: PresentationFramework (在 presentationframework.dll 中)
XML 命名空間:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
/** @property */
public UpdateSourceTrigger get_UpdateSourceTrigger ()

/** @property */
public void set_UpdateSourceTrigger (UpdateSourceTrigger value)

public function get UpdateSourceTrigger () : UpdateSourceTrigger

public function set UpdateSourceTrigger (value : UpdateSourceTrigger)

<object UpdateSourceTrigger="UpdateSourceTrigger" .../>

屬性值

One of the UpdateSourceTrigger values. The default value is Default, which returns the default UpdateSourceTrigger value of the target dependency property. However, the default value for most dependency properties is PropertyChanged, while the Text property has a default value of LostFocus. A programmatic way to determine the default UpdateSourceTrigger value of a dependency property is to get the property metadata of the property using GetMetadata and then check the value of the DefaultUpdateSourceTrigger property.

Bindings that are TwoWay or OneWayToSource listen for changes in the target property and propagate them back to the source. This is known as updating the source. Usually, these updates happen whenever the target property changes. This is fine for check boxes and other simple controls, but it is usually not appropriate for text fields. Updating after every keystroke can diminish performance and it denies the user the usual opportunity to backspace and fix typing errors before committing to the new value. Therefore, the default UpdateSourceTrigger value of the Text property is LostFocus and not PropertyChanged.

If you set the UpdateSourceTrigger value to Explicit, you must call the UpdateSource method or the changes will not propagate back to the source.

This topic describes how to use the UpdateSourceTrigger property to control the timing of binding source updates. The topic uses the TextBox control as an example.

The TextBox.Text property has a default UpdateSourceTrigger value of LostFocus. This means if an application has a TextBox with a data-bound TextBox.Text property, the text you type into the TextBox does not update the source until the TextBox loses focus (for instance, when you click away from the TextBox).

If you want the source to get updated as you are typing, set the UpdateSourceTrigger of the binding to PropertyChanged. In the following example, the Text properties of both the TextBox and the TextBlock are bound to the same source property. The UpdateSourceTrigger property of the TextBox binding is set to PropertyChanged.

<Label>Enter a Name:</Label>
<TextBox>
  <TextBox.Text>
    <Binding Source="{StaticResource myDataSource}" Path="Name"
             UpdateSourceTrigger="PropertyChanged"/>
  </TextBox.Text>
</TextBox>

<Label>The name you entered:</Label>
<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=Name}"/>

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

Simple data binding sample screen shot

For the complete sample, see Simple Binding Sample.

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 UpdateSourceTrigger value of your bindings to Explicit, as in the following example:

<TextBox Name="itemNameTextBox"
         Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />

When you set the UpdateSourceTrigger value to Explicit, the source value only changes when the application calls the UpdateSource method. The following example shows how to call UpdateSource for itemNameTextBox:

// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

For the complete sample, see Explicitly Update Binding Source Sample.

注意事項注意事項:

You can use the same technique for properties of other controls, but keep in mind that most other properties have a default UpdateSourceTrigger value of PropertyChanged. For more information, see the UpdateSourceTrigger property page.

注意事項注意事項:

The UpdateSourceTrigger property deals with source updates and therefore is only relevant for TwoWay or OneWay bindings. For TwoWay and OneWay 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 How to: Implement Property Change Notification.

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支援 Microsoft .NET Framework 3.0。

.NET Framework

支援版本:3.0

社群新增項目

新增
顯示:
© 2014 Microsoft