140 likes | 289 Views
CST238 Week 7. Questions / Concerns? Announcements HW#2 due today (project concept/preliminary design) Check-off Take Home lab#6 Friday is the last day to withdraw from class. GUI Bloopers presentations (#3&#4) New topics Data Binding Coming up:
E N D
CST238 Week 7 • Questions / Concerns? • Announcements • HW#2 due today (project concept/preliminary design) • Check-off Take Home lab#6 • Friday is the last day to withdraw from class. • GUI Bloopers presentations (#3) • New topics • Data Binding • Coming up: • GUI Presentation #5 & #6 next Monday • WPF vs Windows Forms • Work on Final Project • Take Home Lab#7
Why Data Binding? • Windows Forms Problem • There is no clear separation of user interface and data. • Controls and data are tightly coupled. • Data binding doesn’t address this problem of tightly coupled UI and data, but it saves programmers some work: • Don’t have to write code to move data in and out of controls.
Data Binding • One-way: Automatically populating controls with data • Set a few properties • Two-way: Automatically propagating changes to data • From control to data source
Data Binding • Core capability of .NET controls • Web and Windows • Built in at base Control level • Interface-based • An interface contains only the signatures of methods, properties, or events. • A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
Data Binding • Simple data binding • Bind a single value from a data collection to a single control property • Ex: customer name to Text property on Textbox • Complex data binding • Bind collection of data to control that presents multiple values from collection • Ex: customers table in grid, customer name values in combo box.
Data Binding • Simple data binding • Create a Binding object • Add to DataBindings collection of control • Complex data binding • Set data source • Set data member • DisplayMember and ValueMember for ComboBox and List Box controls
C#: Fields & Properties of an object • Fields • Internal data / state of an object. (ex. age) • Fields should not be public. • How do you access the field then? • Through methods (GetAge, SetAge) • Method • Behavior. • Getters/setters are not really behavior though. • Property • Another way to access the field
C#: Property class Student { private int age; //private field public int Age //Public property { get {return age;} set { age = value;} }… Student aStudent= new Student(); aStudent.age = 35; aStudent.Age = 35; //value is 35 if (aStudent.Age == 35) …
C#: Property class Student { private int age; //private field public int Age //Public property { get {return age;} set { age = value;} }… public int Age {get; set;}
Simple Data Binding • Binding object which binds control properties to object properties. aStudent = new Student("Smith", "Gail", "918111111", true); LastnameBox.DataBindings.Add("Text", aStudent, "Lastname"); FirstnameBox.DataBindings.Add("Text", aStudent, "Firstname"); IDBox.DataBindings.Add("Text", aStudent, "ID"); RegisteredCheckbox.DataBindings.Add("Checked", aStudent, "Registered"); Lastname Smith Firstname Gail ID 918111111 Registered true aStudent
Simple Data Binding • Binding object which binds control properties to object properties. aStudent= new Student("Smith", "Gail", "918111111", true); LastnameBox.DataBindings.Add("Text", aStudent, "Lastname“, true, DataSourceUpdateMode.OnPropertyChanged); FirstnameBox.DataBindings.Add("Text", aStudent, "Firstname“, true, DataSourceUpdateMode.OnPropertyChanged); IDBox.DataBindings.Add("Text", aStudent, "ID“, true, DataSourceUpdateMode.Never); RegisteredCheckbox.DataBindings.Add("Checked", aStudent, "Registered“, true, DataSourceUpdateMode.OnPropertyChanged); Lastname Smith Firstname Gail ID 918111111 Registered true
Complex Data Binding • Bind a collection of data to a control • Collection must implement IList or IListSource interface • Student Major List demo • Tab Control • Combo box & binding • DisplayMember (MajorName object property) , ValueMember (MajorCode object property) • DataSource • Listbox, query & binding • DataSource, DisplayMember • Datagrid view • DataSource
Take-Home Lab #7 • Create a Category class with 2 properties: • CategoryID (int) • CategoryName (string) • Create a Product class with 4 properties: • ProductName (string) • CategoryID (int) • Unit Price (float) • OnSale(bool)
Take Home Lab #7 • Create a form: • Combo box to show the list of product categories • Listbox to show the products • Textboxes and checkbox bound to selected product.