Tải bản đầy đủ - 0 (trang)
17-15. Create a Two-Way Binding

17-15. Create a Two-Way Binding

Tải bản đầy đủ - 0trang

CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Table 17-6. BindingMode Values for Configuring the Data Flow in a Binding



Value



Description



Default



The Binding uses the default Mode value of the binding target, which varies for each

dependency property. In general, user-editable control properties, such as those of

text boxes and check boxes, default to two-way bindings, whereas most other

properties default to one-way bindings.



OneTime



The target property is updated when the control is first loaded or when the data

context changes. This type of binding is appropriate if the data is static and won’t

change once it has been set.



OneWay



The target property is updated whenever the source property changes. This is

appropriate if the target control is read-only, such as a

System.Windows.Controls.Label or System.Windows.Controls.TextBlock. If the target

property does change, the source property will not be updated.



OneWayToSource



This is the opposite of OneWay. The source property is updated when the target

property changes.



TwoWay



Changes to either the target property or the source automatically update the other.



Bindings that are TwoWay or OneWayToSource listen for changes in the target property and update the

source. It is the UpdateSourceTrigger property of the binding that determines when this update occurs.

For example, suppose you created a TwoWay binding between the Text property of a TextBox control and

the Value property of a Slider control. You could configure the binding so that the slider is updated

either as soon as you type text into the TextBox or when the TextBox loses its focus. Alternatively, you

could specify that the TextBox is updated only when you explicitly call the UpdateSource property of the

System.Windows.Data.BindingExpression class. These options are configured by the Binding’s

UpdateSourceTrigger property, which stores an instance of the System.Windows.Data.

UpdateSourceTrigger enumeration. Table 17-7 lists the possible values of this enumeration.

Therefore, to create a two-way binding that updates the source as soon as the target property

changes, you need to specify TwoWay as the value of the Binding’s Mode attribute and PropertyChanged for

the UpdateSourceTrigger attribute.



■ Note To detect source changes in OneWay and TwoWay bindings, if the source property is not a System.

Windows.DependencyProperty, it must implement System.ComponentModel.INotifyPropertyChanged to notify

the target that its value has changed.



834



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Table 17-7. UpdateSourceTrigger Values for Configuring When the Binding Source Is Updated



Value



Description



Default



The Binding uses the default UpdateSourceTrigger of the binding target property.

For most dependency properties, this is PropertyChanged, but for the TextBox.Text

property, it is LostFocus.



Explicit



Updates the binding source only when you call the

System.Windows.Data.BindingExpression.UpdateSource method.



LostFocus



Updates the binding source whenever the binding target element loses focus.



PropertyChanged



Updates the binding source immediately whenever the binding target property

changes.



The Code

The following example demonstrates a window containing a System.Windows.Controls.Slider control

and a System.Windows.Controls.TextBlock control. The XAML statement for the Text property of the

TextBlock specifies a Binding statement that binds it to the Value property of the Slider control. In the

binding statement, the Mode attribute is set to TwoWay, and the UpdateSourceTrigger attribute is set to

PropertyChanged. This ensures that when a number from 1 to 100 is typed into the TextBox, the Slider

control immediately updates its value to reflect it. The XAML for the window is as follows:


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Recipe17_15" Height="100" Width="260">




TickFrequency="1" IsSnapToTickEnabled="True"

Minimum="0" Maximum="100"/>




VerticalAlignment="Center" Margin="4"

Text="Gets and sets the value of the slider:" />


Text="{Binding

ElementName=slider,

Path=Value,

Mode=TwoWay,

UpdateSourceTrigger=PropertyChanged}" />









835



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Figure 17-12 shows the resulting window.



Figure 17-12. Creating a two-way binding



17-16. Bind to a Command

Problem

You need to bind a System.Windows.Controls.Button control directly to a System.Windows.Input.

ICommand. This enables you to execute custom logic when the Button is clicked, without having to handle

its Click event and call a method. You can also bind the IsEnabled property of the Button to the ICommand

object’s CanExecute method.



Solution

Create a class that implements ICommand, and expose an instance of it as a property on another class or

business object. Bind this property to a Button control’s Command property.



How It Works

The Button control derives from the System.Windows.Controls.Primitives.ButtonBase class. This

implements the System.Windows.Input.ICommandSource interface and exposes an ICommand property

called Command. The ICommand interface encapsulates a unit of functionality. When its Execute method is

called, this functionality is executed. The CanExecute method determines whether the ICommand can be

executed in its current state. It returns True if the ICommand can be executed and returns False if not.

To execute custom application logic when a Button is clicked, you would typically attach an event

handler to its Click event. However, you can also encapsulate this custom logic in a command and bind

it directly to the Button control’s Command property. This approach has several advantages. First, the

IsEnabled property of the Button will automatically be bound to the CanExecute method of the ICommand.

This means that when the CanExecuteChanged event is fired, the Button will call the command’s

CanExecute method and refresh its own IsEnabled property dynamically. Second, the application

functionality that should be executed when the Button is clicked does not have to reside in the

code-behind for the window. This enables greater separation of presentation and business logic,

which is always desirable in object-oriented programming in general, and even more so in WPF

development, because it makes it easier for UI designers to work alongside developers without getting

in each other’s way.



836



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



To bind the Command property of a Button to an instance of an ICommand, simply set the Path attribute

to the name of the ICommand property, just as you would any other property. You can also optionally

specify parameters using the CommandParameter attribute. This in turn can be bound to the properties of

other elements and is passed to the Execute and CanExecute methods of the command.



The Code

The following example demonstrates a window containing three System.Windows.Controls.TextBox

controls. These are bound to the FirstName, LastName, and Age properties of a custom Person object. The

Person class also exposes an instance of the AddPersonCommand and SetOccupationCommand as read-only

properties. There are two Button controls on the window that have their Command attribute bound to these

command properties. Custom logic in the CanExecute methods of the commands specifies when the

Buttons should be enabled or disabled. If the ICommand can be executed and the Button should therefore

be enabled, the code in the CanExecute method returns True. If it returns False, the Button will be

disabled. The Set Occupation Button control also binds its CommandParameter to the Text property of a

System.Windows.Controls.ComboBox control. This demonstrates how to pass parameters to an instance of

an ICommand. Figure 17-13 shows the resulting window. The XAML for the window is as follows:


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Recipe17_16" Height="233" Width="300">

































837



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

17-15. Create a Two-Way Binding

Tải bản đầy đủ ngay(0 tr)

×