1 / 30

BCIS 4650 Visual Programming for Business Applications

BCIS 4650 Visual Programming for Business Applications. More Data Binding. Brief Review. What is the Binding Source Object?. Any object whose properties contain the value(s) that you want, or A collection of objects. Sometimes a static list Often can be an ObservableCollection <T>

elinor
Download Presentation

BCIS 4650 Visual Programming for Business Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BCIS 4650 Visual Programming for Business Applications More Data Binding The University of North Texas, ITDS Dept., Dr. Vedder

  2. Brief Review

  3. What is the Binding Source Object? • Any object whose properties contain the value(s) that you want, or • A collection of objects The University of North Texas, ITDS Dept., Dr. Vedder

  4. Sometimes a static list • Often can be an ObservableCollection<T> • Must support INotifyPropertyChanged to update UI • Must have a Dependency Property • DP name format is always nnnProperty • Often “shortcut syntax” used • Use Path= to retrieve just a property from the source object The University of North Texas, ITDS Dept., Dr. Vedder

  5. What is the Binding Target Object? • Must be a class that have one or more dependent properties • Often children of ItemsControl class • TextBox and other textual controls The University of North Texas, ITDS Dept., Dr. Vedder

  6. Children of ItemsControl Class include • Most popular: ListView, GridView, ListBox • FlipView • ComboBox • MenuFlyoutPresenter, Selector • (Could use IC directly, but children have more custom features) The University of North Texas, ITDS Dept., Dr. Vedder

  7. Simple Binding

  8. Simple Binding Example in XAML • Source object = MyData • Source property = ColorName • Target object = Button • Target DP = Background(.Property) (Button inherits DataContext value) The University of North Texas, ITDS Dept., Dr. Vedder

  9. Different Syntaxes; Same Result • Explicitly calls path binding property • Exploits ButtonTextas Button’s default property • Uses “fully qualified” binding syntax The University of North Texas, ITDS Dept., Dr. Vedder

  10. The Path Attribute/Prop. of Bindingcommon syntax; not for XML-formatted data files • Simplest: Path=propertyname • Attached: Path=(object.property) • Indexed: Path=propertyname[n] • If source object has only one value, not necessary to use Path The University of North Texas, ITDS Dept., Dr. Vedder

  11. The XPath Attribute/Property • Use for binding to XML data files • http://msdn.microsoft.com/en-us/library/system.windows.data.binding.xpath%28v=vs.110%29.aspx The University of North Texas, ITDS Dept., Dr. Vedder

  12. FYI:Sample XML Data File ~ .JSONData File(human-formatted)

  13. List<T>ObservableCollection<T>

  14. List<T> Class (T = element type) • Is a strongly typed list of objects • Accessed by index value • Has methods to search, sort, manipulate • Is read-only; does not update UI The University of North Texas, ITDS Dept., Dr. Vedder

  15. ObservableCollection<T> (T=type) a special version of the more generic Collection<T> • Is a generic, dynamic data collection • Supports additions, deletions, updates • To use effectively in XAML, create in C# a custom collection class that inherits from ObservableCollection<T>, and then reference it in XAML The University of North Texas, ITDS Dept., Dr. Vedder

  16. Example: Data for a ListBox

  17. Displaying Data in a ListBox • Populate with XAML elements • Populate using Binding class and Path= or XPath= • Populate by setting ItemsSource dep. property to a collection of data and, if needed, setting DisplayMemberPath to the field you want retrieved The University of North Texas, ITDS Dept., Dr. Vedder

  18. 1. C#: Create a NameList Collection Class & PersonName Type NameList collection class, inherits from OC of type PersonName Defines the type as a class Constructor for the type Exposed variable Exposed variable

  19. 2. XAML: Instantiate Collection; Then Specify It as a Resource SDKSample is a custom CLR namespace defined in C# Partial class directive Use the NameList class found in the SDKSample namespace and give it the key NameListData The University of North Texas, ITDS Dept., Dr. Vedder

  20. 3. XAML: Bind the Collection • Add “Path=” above if you want just FirstName or LastName • (path example with different collection) The University of North Texas, ITDS Dept., Dr. Vedder

  21. 1. Using ItemsSource & DisplayMemberPathCustomer type class, Customers collection class Defines the type as a class with 3 exposed variables Constructor for the type Customers collection class, inherits from OC of type Customer

  22. 2. Using ItemsSource & DisplayMemberPath(retrieves Anderberg, Ashton, Hicks, Pica) ListBoxSnippetEx is a custom CLR namespace defined in C# Use the Customers class inside ListBoxSnippetExnamespace and give it the key customers The University of North Texas, ITDS Dept., Dr. Vedder

  23. Monitoring Collection Changes • The ObservableCollection<T> class implements INotifyCollectionChangedinterface. • This exposes a CollectionChanged event that you can use when the custom collection class values change. The University of North Texas, ITDS Dept., Dr. Vedder

  24. Data Templates

  25. Data Templates • Specify the presentation or visual structure of associated data objects • Used often when binding items, ex., a ListBox to a collection of data objects • Can be written individually, or as a shared resource • Use data binding to adapt a data template to the needs of each element The University of North Texas, ITDS Dept., Dr. Vedder

  26. Data Templates ≠ Control Templates • Control template: defines look and some behavior of a control, including control state behavior (focus, hover, …) • Data template: defines look for a specific property of a control; far more atomic The University of North Texas, ITDS Dept., Dr. Vedder

  27. ListBox Without a Data Templateabbreviated example; see http://msdn.microsoft.com/en-us/library/ms742521%28v=vs.110%29.aspx The University of North Texas, ITDS Dept., Dr. Vedder

  28. ListBox with a DataTemplateabbreviated example; see http://msdn.microsoft.com/en-us/library/ms742521%28v=vs.110%29.aspx

  29. Some Other Data Template Examples(same source as before) The University of North Texas, ITDS Dept., Dr. Vedder

  30. ITDS Logo / Mood Slide The University of North Texas, ITDS Dept., Dr. Vedder

More Related