tailieunhanh - Lập trình .net 4.0 và visual studio 2010 part 54

Như đã đề cập trước đó, dữ liệu ràng buộc sử dụng bất động sản phụ thuộc để duy trì mối quan hệ giữa đối tượng và nguồn dữ liệu. Nếu bạn thay đổi một mục đó là databound sau đó bất kỳ điều khiển khác ràng buộc với các mục dữ liệu sẽ thay đổi là tốt. Hãy tạo ra một ví dụ để chứng minh điều này. | CHAPTER 14 SILVERLIGHT INTRODUCTION DataBinding Modes In the preceding example you bound a TextBox to the Title property of Movie and defined the binding mode as one way TextBox x Name txtDeclaration Width 300 Text Binding Title Mode OneWay Height 20 TextBox Silverlight offers three binding modes OneTime This is the lightweight option and should be used if the value will never change from what it is initialized as. OneWay If the item the object is bound to is changed the bound property will be updated as well. If you change the bound property it will not affect the object it is bound to. TwoWay Like OneWay binding but any changes to the bound property will also alter the bound object. Data Binding and Dependency Properties As mentioned earlier data binding uses dependency properties to maintain the relationship between the object and datasource. If you change an item that is databound then any other controls bound to the data item will change as well. Let s create an example to demonstrate this. 1. Open DataBinding . 2. Add the following XAML after the TextBox that has the x name set to txtDeclaration TextBlock Change an object in list TextBlock Button x Name cmdChangeTitle Width 100 Height 20 Content Change Title Button 3. Open and add a click event handler new RoutedEventHandler cmdChangeTitle_Click 4. Add a method to handle the click void cmdChangeTitle_Click object sender RoutedEventArgs e Change an item in the list MoviesList 0 .Title Title Changed 5. Press F5 to run the application. Click the button to change the movie title. The text box that was bound to this item will then change. Note that the contents of the text box that is bound was automatically updated when you changed the class it was bound to. 359 CHAPTER 14 SILVERLIGHT INTRODUCTION Two-Way Binding You will now create an example to demonstrate two-way data binding. 1. Open . 2. Add the following XAML after the