300 likes | 445 Views
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>
E N D
BCIS 4650 Visual Programming for Business Applications More Data Binding The University of North Texas, ITDS Dept., Dr. Vedder
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
ListBox with a DataTemplateabbreviated example; see http://msdn.microsoft.com/en-us/library/ms742521%28v=vs.110%29.aspx
Some Other Data Template Examples(same source as before) The University of North Texas, ITDS Dept., Dr. Vedder
ITDS Logo / Mood Slide The University of North Texas, ITDS Dept., Dr. Vedder