void AddCollectionBinding(CollectionPresenter collectionPresenter, string sourcePropName) { OneWayCollectionValueBindingWithPath<Person, IPresentationTreeNode, IEnumerable<Person>, ObservableCollection<IPresentationTreeNode>> collectionFieldBinding = new OneWayCollectionValueBindingWithPath<Person, IPresentationTreeNode, IEnumerable<Person>, ObservableCollection<IPresentationTreeNode>>(); collectionFieldBinding.SourceObj = TheClass; collectionFieldBinding.SourcePathLinks = new List<BindingPathLink<object>> { new BindingPathLink<object>(sourcePropName) }; collectionFieldBinding.TargetObj = collectionPresenter; collectionFieldBinding.TargetPathLinks = new List<BindingPathLink<object>> { new BindingPathLink<object>("ObservableChildren") }; collectionFieldBinding.SourceToTargetDelegate = GetPersonMap; collectionFieldBinding.Bind(); TheBindingsAProp.SetProperty(collectionPresenter, collectionFieldBinding); }
static void Main(string[] args) { #region pure collection binding //ObservableCollection<int> sourceCollection = // new ObservableCollection<int> { 1, 2, 3 }; //ObservableCollection<string> targetCollection = // new ObservableCollection<string>(); //TwoWayCollectionBinding<int, string> twoWayBinding = new TwoWayCollectionBinding<int, string> //{ // SourceCollection = sourceCollection, // TargetCollection = targetCollection, // SourceToTargetDelegate = (i) => i.ToString(), // TargetToSourceDelegate = (str) => Int32.Parse(str) //}; //twoWayBinding.Bind(); //Console.WriteLine("\nOriginal source collection"); //sourceCollection.ForEach((i) => Console.WriteLine(i)); // print the source collection //Console.WriteLine("\nOriginal target collection"); //targetCollection.ForEach((i) => Console.WriteLine(i)); // print the source collection //Console.WriteLine("\nAfter removing element at index 1 from source"); //sourceCollection.RemoveAt(1); // remove element at index 1 from source collection //targetCollection.ForEach((str) => Console.WriteLine(str)); // print target collection //Console.WriteLine("\nAfter adding 4 to target"); //targetCollection.Add("4"); // append string "4" to the end of the target collection //sourceCollection.ForEach((i) => Console.WriteLine(i)); // print the source collection //Console.WriteLine("\nAfter inserting 0 to target"); //targetCollection.Insert(0, "0"); // insert string "0" at index 0 for the target collection //sourceCollection.ForEach((i) => Console.WriteLine(i)); // print the source collection //Console.WriteLine("\nAfter inserting 2 at index 2 to source"); //sourceCollection.Insert(2, 2); // insert number 2 at index 2 for the source collection //targetCollection.ForEach((str) => Console.WriteLine(str)); // print the target collection #endregion pure collection binding #region collection value binding //Console.WriteLine("\nValue collection bindings"); //TestData<int> source = new TestData<int>(); //TestData<string> target = new TestData<string>(); //OneWayCollectionValueBindingWithPath //< // int, // string, // ObservableCollection<int>, // ObservableCollection<string> //> //theBinding = new OneWayCollectionValueBindingWithPath // < // int, // string, // ObservableCollection<int>, // ObservableCollection<string> // > // { // IsOneWayCollectionBinding = false, // CreationType = typeof(ObservableCollection<string>), // SourceObj = source, // TargetObj = target, // SourceToTargetDelegate = (i) => i.ToString(), // TargetToSourceDelegate = (str) => Int32.Parse(str) // }; //List<BindingPathLink<object>> sourcePathLinks = new List<BindingPathLink<object>>(); //sourcePathLinks.Add(new BindingPathLink<object>("TheCollection")); //theBinding.SourcePathLinks = sourcePathLinks; //List<BindingPathLink<object>> targetPathLinks = new List<BindingPathLink<object>>(); //targetPathLinks.Add(new BindingPathLink<object>("TheCollection")); //theBinding.TargetPathLinks = targetPathLinks; //theBinding.Bind(); //source.TheCollection = new ObservableCollection<int>(new int[] { 1, 2, 3 }); //target.TheCollection.ForEach((str) => Console.WriteLine(str)); // print the target collection //source.TheCollection.RemoveAt(1); //Console.WriteLine(); //target.TheCollection.ForEach((str) => Console.WriteLine(str)); // print the target collection //target.TheCollection.Add("4"); //Console.WriteLine(); //source.TheCollection.ForEach((str) => Console.WriteLine(str)); // print the source collection #endregion collection value binding #region value binding type testing //TestData<Person> sourcePeople = new TestData<Person>(); //TestData<Person> targetPeople = new TestData<Person>(); //OneWayCollectionValueBindingWithPath peopleBinding = new OneWayCollectionValueBindingWithPath //{ // SourceObj = sourcePeople, // TargetObj = targetPeople, // IsOneWayCollectionBinding = false, // CreationType = typeof(ObservableCollection<Person>) //}; //List<BindingPathLink<object>> peopleSourcePathLinks = new List<BindingPathLink<object>>(); //peopleSourcePathLinks.Add(new BindingPathLink<object>("TheCollection")); //peopleBinding.SourcePathLinks = peopleSourcePathLinks; //List<BindingPathLink<object>> peopleTargetPathLinks = new List<BindingPathLink<object>>(); //peopleTargetPathLinks.Add(new BindingPathLink<object>("TheCollection")); //peopleBinding.TargetPathLinks = peopleTargetPathLinks; //peopleBinding.Bind(); //UniversityClass universityClass = new UniversityClass(); //sourcePeople.TheCollection = universityClass.People; #endregion value binding type testing #region Map Collection Tests UniversityClass theUniversityClass = new UniversityClass(); DHierarchyItemsControl dHierarchyItemsControl = new DHierarchyItemsControl(); OneWayCollectionValueBindingWithPath itemsSourceBinding = new OneWayCollectionValueBindingWithPath { SourceObj = dHierarchyItemsControl, TargetObj = dHierarchyItemsControl, IsOneWayCollectionBinding = true }; List<BindingPathLink<object>> dataContextSourcePathLinks = new List<BindingPathLink<object>>(); dataContextSourcePathLinks.Add(new BindingPathLink<object>("DataContext")); dataContextSourcePathLinks.Add(new BindingPathLink<object>("People")); itemsSourceBinding.SourcePathLinks = dataContextSourcePathLinks; List<BindingPathLink<object>> itemsSourceTargetPathLinks = new List<BindingPathLink<object>>(); itemsSourceTargetPathLinks.Add(new BindingPathLink<object>("ItemsSource")); itemsSourceBinding.TargetPathLinks = itemsSourceTargetPathLinks; itemsSourceBinding.Bind(); dHierarchyItemsControl.DataContext = theUniversityClass; #endregion Map Collection Tests int i = 0; }
static void Main() { // source object Organization myOrg = new Organization { OrgName = "TheOrganization" }; // target object OrgPrintModel orgPrintModel = new OrgPrintModel(); // create the collection binding with path OneWayCollectionValueBindingWithPath peopleBinding = new OneWayCollectionValueBindingWithPath { // set the source object SourceObj = myOrg, // define the path to the source property // with respect to the source object SourcePathLinks = new BindingPathLink<object>[] { new BindingPathLink<object>("People") }, // set the target object TargetObj = orgPrintModel, // define the path to the target property // with respect to the target object TargetPathLinks = new BindingPathLink<object>[] { new BindingPathLink<object>("ItemsToPrint") }, // creates the target collection TheSourceToTargetValueConverterDelegate = (sourceCollection) => { return new List<PrintItem>(); }, // converts source collection item to target collection item SourceToTargetItemDelegate = (sourceItem) => { OrgPerson orgPerson = (OrgPerson)sourceItem; return new PrintItem { StringToPrint = orgPerson.Name + " " + orgPerson.ThePosition.ToString() }; } }; // do the binding peopleBinding.Bind(); // check that the there are no items in the // output collection before the source collection // is set and populated orgPrintModel.Print("Before the people collection is populated:"); // create the input collection myOrg.People = new ObservableCollection<OrgPerson>(); // populate the source collection with two positions myOrg.People.Add(new OrgPerson("Tom", Position.CEO)); myOrg.People.Add(new OrgPerson("John", Position.Manager)); // verify that the binding target collection was // modified accordingly orgPrintModel.Print("Make sure the binding makes the target collection to be in synch with the source collection:"); // add another person to the source collection myOrg.People.Add(new OrgPerson("Nick", Position.Developer)); // verify that the new item 'Nick' was added to target collection orgPrintModel.Print("After adding 'Nick Developer' item to the source collection, make sure it also appears in the target collection:"); // remove Tom/CEO item from the source collection and make sure that // the target collection is updated correspondingly OrgPerson ceo = myOrg.People.Where((person) => person.ThePosition == Position.CEO).FirstOrDefault(); myOrg.People.Remove(ceo); orgPrintModel.Print("After removing the CEO item from the source collection make sure it also disappeared from the target collection:"); // null the whose source collection and make sure that // the target collection is also null or empty myOrg.People = null; orgPrintModel.Print("After source collection is null, target collection should be null or empty:"); // check that when you replace myOrg.People source collection // with a new (already populated) collection, // the target collection will contain the corresponding items. ObservableCollection<OrgPerson> newPeopleCollection = new ObservableCollection<OrgPerson>(); newPeopleCollection.Add(new OrgPerson("Tom", Position.CEO)); newPeopleCollection.Add(new OrgPerson("John", Position.Manager)); myOrg.People = newPeopleCollection; orgPrintModel.Print("After source collection is reset, target collection should also be reset:"); }