MSDN Magazine - December 2008 - (Page 25) that default view of the collection within the ListBox controls. You could create multiple CollectionView instances based on the same collection and display the data in different ways (sorted, for example) within one of the ListBox controls. Although it’s outside the scope of this discussion, it’s worth investigating the CollectionView class if you need to display multiple views of the same collection. For more information, please look at msdn.microsoft.com/library/system.winFigure 4 Capturing Change Events for Data in the Collection dows.data.collectionview. Follow these steps to give the sample application a workout. 1. Within Visual Studio 2008, load and run the sample application. You’ll see two instances of the same window. 2. Click to open the combobox on the left side of the window. Note that the control contains the numbers 0 through 2, corresponding to the three current customers. Choose 1, which selects Ana Trujillo and copies her name into the textbox. 3. In one window, click Delete. Ana Trujillo disappears from both windows, because the two ListBox controls are bound to the same ObservableCollection instance, and the binding causes updates to appear immediately. Open the combobox again, and note that it now indicates that there are only two customers. Try it in both windows to verify that both instances are up-to-date. 4. Click Delete two more times, removing all the customers. Click Reset Data to refill the lists in both windows. 5. In the textbox next to the Add New Item button, enter your own name and click Add New Item. The new name appears in both ListBox controls. Click to open the combobox and verify that it now contains numbers from 0 to 3 (one for each of the four customers). Verify that the combobox has changed on both windows—obviously, both windows’ classes received the event that indicated that the collection had changed. 6. In the ListBox in one window, select a name. In the lower textbox on the left, modify the name and click Change. First, you’ll see an alert indicating that you have changed the property, and once you dismiss it, you’ll see that the name has changed in both windows (see Figure 4). Except for the alert that appeared when you changed the customer name and the ComboBox control whose items change to match the number of customers in the collection, all the code in the sample window deals with the window’s user interface. That is, all the work done to keep the ListBox control in sync with the ObservableCollection instance happens for “free” and is managed by WPF. When you add a new item, the ListBox automatically displays the full list. When you change an item, the ListBox automatically displays the modified list. When you delete items, the ListBox maintains full fidelity with the underlying collection. In other words, in terms of binding the ObservableCollection class to controls in WPF, you can easily say “It just works.” Actually, there is some “magic” going on under the covers. When you bind the ListBox to the ObservableCollection, WPF actually creates a CollectionView instance, which presents a view of the data that handles grouping, sorting, filtering, and so on. You’re seeing msdnmagazine.com Examining the Code The MainWindow class defines the CustomerList instance using the WithEvents keyword so that the code can handle events of the ObservableCollection list without requiring the code to add handlers manually: Public WithEvents Data As CustomerList = CustomerList.GetList() In the Change Event Handlers region of the code, you’ll find the CollectionChanged event handler, which verifies that you have either added or removed an item from the collection. If so, the code sets the combobox’s data source and enables buttons on the window accordingly, as you see in Figure 5. The important part of this simple code lies in the NotifyCollecFigure 5 Check for Collection Changed Private Sub Data_CollectionChanged( _ ByVal sender As Object, _ ByVal e As NotifyCollectionChangedEventArgs) _ Handles Data.CollectionChanged ' ' ' ' Because the collection raises this event, you can modify your user interface on any window that displays controls bound to the data. On both windows, if you add or remove an item, all the controls update to indicate the new collection! ' Did you add or remove an item in the collection? If e.Action = NotifyCollectionChangedAction.Add Or _ e.Action = NotifyCollectionChangedAction.Remove Then ' Set the list of integers in the combo box: SetComboDataSource() ' Enable buttons as necessary: EnableButtons() End If End Sub Figure 6 NotifyCollectionChangedEventArgs Parameters Parameter Action Description Retrieves information about the action that caused the event. This property contains a NotifyCollectionChangedAction value, which can be either Add, Remove, Replace, Move, or Reset. Retrieves a list of the new items that were involved in the change to the collection. Retrieves the index of the collection at which the change occurred. Retrieves a list of the old items that were affected by the Replace, Remove, or Move action. Retrieves the index of the collection at which the Replace, Remove, or Move action occurred. December 2008 25 NewItems NewStartingIndex OldItems OldStartingIndex http://msdn.microsoft.com/library/system.windows.data.collectionview http://msdn.microsoft.com/library/system.windows.data.collectionview http://www.msdnmagazine.com
Table of Contents Feed for the Digital Edition of MSDN Magazine - December 2008 MSDN Magazine - December 2008 Contents Toolbox CLR Inside Out Advanced Basics Cutting Edge Patterns In Practice Team System Real-World WF Visual Studio OBA Tools SOA Data Access Geneva Framework Test Run Foundations Windows With C++ Going Places End Bracket MSDN Magazine - December 2008 MSDN Magazine - December 2008 - (Page Intro) MSDN Magazine - December 2008 - Contents (Page Cover1) MSDN Magazine - December 2008 - Contents (Page Cover2) MSDN Magazine - December 2008 - Contents (Page 1) MSDN Magazine - December 2008 - Contents (Page 2) MSDN Magazine - December 2008 - Contents (Page 3) MSDN Magazine - December 2008 - Contents (Page 4) MSDN Magazine - December 2008 - Contents (Page 5) MSDN Magazine - December 2008 - Contents (Page 6) MSDN Magazine - December 2008 - Contents (Page 7) MSDN Magazine - December 2008 - Contents (Page 8) MSDN Magazine - December 2008 - Contents (Page 9) MSDN Magazine - December 2008 - Contents (Page 10) MSDN Magazine - December 2008 - Toolbox (Page 11) MSDN Magazine - December 2008 - Toolbox (Page 12) MSDN Magazine - December 2008 - Toolbox (Page 13) MSDN Magazine - December 2008 - Toolbox (Page 14) MSDN Magazine - December 2008 - CLR Inside Out (Page 15) MSDN Magazine - December 2008 - CLR Inside Out (Page 16) MSDN Magazine - December 2008 - CLR Inside Out (Page 17) MSDN Magazine - December 2008 - CLR Inside Out (Page 18) MSDN Magazine - December 2008 - CLR Inside Out (Page 19) MSDN Magazine - December 2008 - CLR Inside Out (Page 20) MSDN Magazine - December 2008 - CLR Inside Out (Page 21) MSDN Magazine - December 2008 - Advanced Basics (Page 22) MSDN Magazine - December 2008 - Advanced Basics (Page 23) MSDN Magazine - December 2008 - Advanced Basics (Page 24) MSDN Magazine - December 2008 - Advanced Basics (Page 25) MSDN Magazine - December 2008 - Advanced Basics (Page 26) MSDN Magazine - December 2008 - Advanced Basics (Page 27) MSDN Magazine - December 2008 - Advanced Basics (Page 28) MSDN Magazine - December 2008 - Cutting Edge (Page 29) MSDN Magazine - December 2008 - Cutting Edge (Page 30) MSDN Magazine - December 2008 - Cutting Edge (Page 31) MSDN Magazine - December 2008 - Cutting Edge (Page 32) MSDN Magazine - December 2008 - Cutting Edge (Page 33) MSDN Magazine - December 2008 - Cutting Edge (Page 34) MSDN Magazine - December 2008 - Cutting Edge (Page 35) MSDN Magazine - December 2008 - Cutting Edge (Page 36) MSDN Magazine - December 2008 - Patterns In Practice (Page 37) MSDN Magazine - December 2008 - Patterns In Practice (Page 38) MSDN Magazine - December 2008 - Patterns In Practice (Page 39) MSDN Magazine - December 2008 - Patterns In Practice (Page 40) MSDN Magazine - December 2008 - Patterns In Practice (Page 41) MSDN Magazine - December 2008 - Patterns In Practice (Page 42) MSDN Magazine - December 2008 - Patterns In Practice (Page 43) MSDN Magazine - December 2008 - Team System (Page 44) MSDN Magazine - December 2008 - Team System (Page 45) MSDN Magazine - December 2008 - Team System (Page 46) MSDN Magazine - December 2008 - Team System (Page 47) MSDN Magazine - December 2008 - Team System (Page 48) MSDN Magazine - December 2008 - Team System (Page 49) MSDN Magazine - December 2008 - Team System (Page 50) MSDN Magazine - December 2008 - Team System (Page 51) MSDN Magazine - December 2008 - Real-World WF (Page 52) MSDN Magazine - December 2008 - Real-World WF (Page 53) MSDN Magazine - December 2008 - Real-World WF (Page 54) MSDN Magazine - December 2008 - Real-World WF (Page 55) MSDN Magazine - December 2008 - Real-World WF (Page 56) MSDN Magazine - December 2008 - Real-World WF (Page 57) MSDN Magazine - December 2008 - Real-World WF (Page 58) MSDN Magazine - December 2008 - Real-World WF (Page 59) MSDN Magazine - December 2008 - Real-World WF (Page 60) MSDN Magazine - December 2008 - Real-World WF (Page 61) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 62) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 63) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 64) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 65) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 66) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 67) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 68) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 69) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 70) MSDN Magazine - December 2008 - Visual Studio OBA Tools (Page 71) MSDN Magazine - December 2008 - SOA Data Access (Page 72) MSDN Magazine - December 2008 - SOA Data Access (Page 73) MSDN Magazine - December 2008 - SOA Data Access (Page 74) MSDN Magazine - December 2008 - SOA Data Access (Page 75) MSDN Magazine - December 2008 - SOA Data Access (Page 76) MSDN Magazine - December 2008 - SOA Data Access (Page 77) MSDN Magazine - December 2008 - SOA Data Access (Page 78) MSDN Magazine - December 2008 - SOA Data Access (Page 79) MSDN Magazine - December 2008 - SOA Data Access (Page 80) MSDN Magazine - December 2008 - SOA Data Access (Page 81) MSDN Magazine - December 2008 - Geneva Framework (Page 82) MSDN Magazine - December 2008 - Geneva Framework (Page 83) MSDN Magazine - December 2008 - Geneva Framework (Page 84) MSDN Magazine - December 2008 - Geneva Framework (Page 85) MSDN Magazine - December 2008 - Geneva Framework (Page 86) MSDN Magazine - December 2008 - Geneva Framework (Page 87) MSDN Magazine - December 2008 - Geneva Framework (Page 88) MSDN Magazine - December 2008 - Geneva Framework (Page 89) MSDN Magazine - December 2008 - Geneva Framework (Page 90) MSDN Magazine - December 2008 - Test Run (Page 91) MSDN Magazine - December 2008 - Test Run (Page 92) MSDN Magazine - December 2008 - Test Run (Page 93) MSDN Magazine - December 2008 - Test Run (Page 94) MSDN Magazine - December 2008 - Test Run (Page 95) MSDN Magazine - December 2008 - Test Run (Page 96) MSDN Magazine - December 2008 - Test Run (Page 97) MSDN Magazine - December 2008 - Test Run (Page 98) MSDN Magazine - December 2008 - Test Run (Page 99) MSDN Magazine - December 2008 - Test Run (Page 100) MSDN Magazine - December 2008 - Foundations (Page 101) MSDN Magazine - December 2008 - Foundations (Page 102) MSDN Magazine - December 2008 - Foundations (Page 103) MSDN Magazine - December 2008 - Foundations (Page 104) MSDN Magazine - December 2008 - Foundations (Page 105) MSDN Magazine - December 2008 - Foundations (Page 106) MSDN Magazine - December 2008 - Foundations (Page 107) MSDN Magazine - December 2008 - Foundations (Page 108) MSDN Magazine - December 2008 - Windows With C++ (Page 109) MSDN Magazine - December 2008 - Windows With C++ (Page 110) MSDN Magazine - December 2008 - Windows With C++ (Page 111) MSDN Magazine - December 2008 - Windows With C++ (Page 112) MSDN Magazine - December 2008 - Going Places (Page 113) MSDN Magazine - December 2008 - Going Places (Page 114) MSDN Magazine - December 2008 - Going Places (Page 115) MSDN Magazine - December 2008 - Going Places (Page 116) MSDN Magazine - December 2008 - Going Places (Page 117) MSDN Magazine - December 2008 - Going Places (Page 118) MSDN Magazine - December 2008 - Going Places (Page 119) MSDN Magazine - December 2008 - End Bracket (Page 120) MSDN Magazine - December 2008 - End Bracket (Page Cover3) MSDN Magazine - December 2008 - End Bracket (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.