XPO – 11.1 Sneak Peek – Data binding in WPF

16 May 2011

In this blog post, we are going to demonstrate how to use a new XPObservableAssociationList<T> class in the DevExpress.Xpo.Linq library for data binding to persistent classes in WPF.

First, we will create a new WPF Application project. Then, we will add references to the DevExpress.Data, DevExpress.Xpo and DevExpress.Xpo.Linq assemblies. Then, we will add a base persistent BaseObject and a couple of persistent classes Person and Order as follows:

  1: using DevExpress.Xpo;
  2: using DevExpress.Xpo.DB;
  3: using DevExpress.Xpo.Helpers;
  4: …
  5: [NonPersistent]
  6: public class BaseObject : PersistentBase {
  7:     int oid;
  8:     [Key(true)]
  9:     public int Oid {
 10:         get { return oid; }
 11:         set { SetPropertyValue<int>("Oid", ref oid, value); }
 12:     }
 13:     protected override IList<T> CreateAssociationList<T>(DevExpress.Xpo.Metadata.XPMemberInfo property) {
 14:         return new XPObservableAssociationList<T>(Session, this, property);
 15:     }
 16:     public BaseObject(Session session)
 17:         : base(session) {
 18:     }
 19: }
 20: public class Person : BaseObject {
 21:     string firstName;
 22:     public string FirstName {
 23:         get { return firstName; }
 24:         set { SetPropertyValue<string>("FirstName", ref firstName, value); }
 25:     }
 26:     string lastName;
 27:     public string LastName {
 28:         get { return lastName; }
 29:         set { SetPropertyValue<string>("LastName", ref lastName, value); }
 30:     }
 31:     int age;
 32:     public int Age {
 33:         get { return age; }
 34:         set { SetPropertyValue<int>("Age", ref age, value); }
 35:     }
 36:     [Association("Person-Orders")]
 37:     public IList<Order> Orders {
 38:         get { return GetList<Order>("Orders"); }
 39:     }
 40:     public Person(Session sesson) : base(sesson) { }
 41: }
 42: public class Order : BaseObject {
 43:     DateTime orderDate;
 44:     public DateTime OrderDate {
 45:         get { return orderDate; }
 46:         set { SetPropertyValue<DateTime>("OrderDate", ref orderDate, value); }
 47:     }
 48:     Person employee;
 49:     [Association("Person-Orders")]
 50:     public Person Employee {
 51:         get { return employee; }
 52:         set { SetPropertyValue<Person>("Employee", ref employee, value); }
 53:     }
 54:     public Order(Session session) : base(session) { }
 55:     public override string ToString() {
 56:         return string.Format("Order - {0};", OrderDate);
 57:     }
 58: }
 59: 

Now, let’s drop the ListBox (listBox1) and Button (button1) components onto the form and handle the Click event for button1. Additionally, we will modify the form’s code as follows:

  1: using DevExpress.Xpo;
  2: using DevExpress.Xpo.DB;
  3: using DevExpress.Xpo.Helpers;
  4: ...
  5: public partial class Window1 : Window {
  6:     UnitOfWork uowCommon;
  7:     Person personToBind;
  8:     public Window1() {
  9:         InitializeComponent();
 10:         XpoDefault.DataLayer = new SimpleDataLayer(
 11:             new InMemoryDataStore(AutoCreateOption.DatabaseAndSchema)
 12:         );
 13:         XpoDefault.Session = null;
 14:         PrepareObjects();
 15:         uowCommon = new UnitOfWork();
 16:         personToBind = uowCommon.FindObject<Person>(null);
 17:         if(personToBind != null){
 18:             listBox1.ItemsSource = personToBind.Orders;
 19:         }
 20:     }
 21:     private void button1_Click(object sender, RoutedEventArgs e) {
 22:         if(personToBind != null && personToBind.Orders.Count > 0){
 23:             personToBind.Orders[0].Employee = null;
 24:         }
 25:     }
 26:     void PrepareObjects() {
 27:         using (UnitOfWork uow = new UnitOfWork()) {
 28:             Person p = new Person(uow);
 29:             p.FirstName = "Jhon";
 30:             p.LastName = "Smith";
 31:             p.Age = 25;
 32:             DateTime now = DateTime.Now;
 33:             for (int i = 0; i < 10; i++) {
 34:                 Order o = new Order(uow);
 35:                 o.Employee = p;
 36:                 o.OrderDate = now.AddDays(i).AddSeconds(i * 12312);
 37:             }
 38:             uow.CommitChanges();
 39:         }
 40:     }
 41: }
 42: 

Now we are done. If we run the application, we will see the following results:

Happy XPOing!Winking smile

2 comment(s)
Joe Hendricks

Great demo - simple and clear!

16 May, 2011
Demetrios Seferlis

I cannot find XPObservableAssociationList. What is the assembly containing this?

8 April, 2012

Please login or register to post comments.