Scheduler for UWP | ComponentOne
Features / Data Binding / Appointment Binding
In This Topic
    Appointment Binding
    In This Topic

    Scheduler for UWP allows binding to a custom data source. Using NestedPropertySetter, you'll set the mappings between the appointment properties and the AppointmentStorage class by setting the AppointmentStorage.DataSource property.

    Start with a project to which you've added the appropriate references and a C1Scheduler control.

     

     The initial namespace declarations and XAML markup should resemble the following sample:

    XAML
    Copy Code
    <Page
        x:Class="ScheduleSamples.Samples.BusinessObjectsBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:ScheduleSamples.Samples"
        xmlns:Schedule="using:C1.Xaml.Schedule"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Schedule:C1Scheduler x:Name="sched1"  >            
            </Schedule:C1Scheduler>
        </Grid>
    </Page>
    

    Right-click the page and select View Code from the context menu. When your code file opens, check your namespace declarations to make sure they match the following:

    C#
    Copy Code
    using C1.C1Schedule;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Runtime.Serialization;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    

    Then edit your MainPage() constructor to add a demo appointment to your AppointmentCollection:

    C#
    Copy Code
         public MainPage()
         {
             this.InitializeComponent();
             sched1.Settings.FirstVisibleTime = System.TimeSpan.FromHours(8);
             AppointmentCollection apps = Resources["_ds"] as AppointmentCollection;
             if (apps != null)
             {
                 // add demo appointment
                 Appointment app = new Appointment();
                 app.Subject = "test appointment";
                 app.Start = DateTime.Today;
                 apps.Add(app);
             }
         }
     }
    

    Then, add a custom Appointment class and the PropertyChangedEventHandler:

    C#
    Copy Code
    [DataContract(Name = "Appointment", Namespace = "https://www.grapecity.com")]
    public class Appointment : INotifyPropertyChanged
    {
        public Appointment()
        {
            Id = Guid.NewGuid();
        }
        [DataMember]
        public Guid Id { get; private set; }
        private string _subject = "";
        [DataMember]
        public string Subject
        {
            get { return _subject; }
            set
            {
                if (_subject != value)
                {
                    _subject = value;
                    OnPropertyChanged("Subject");
                }
            }
        }
        private string _location = "";
        [DataMember]
        public string Location
        {
            get { return _location; }
            set
            {
                if (_location != value)
                {
                    _location = value;
                    OnPropertyChanged("Location");
                }
            }
        }
        private DateTime _start;
        [DataMember]
        public DateTime Start
        {
            get { return _start; }
            set
            {
                if (_start != value)
                {
                    _start = value;
                    OnPropertyChanged("Start");
                }
            }
        }
        [DataMember]
        public DateTime End
        {
            get { return _start.Add(_duration); }
            set
            {
                if (value >= _start)
                {
                    Duration = (value.Subtract(_start));
                    OnPropertyChanged("End");
                }
            }
        }
        private TimeSpan _duration;
        public TimeSpan Duration
        {
            get { return _duration; }
            set
            {
                if (_duration != value)
                {
                    _duration = value;
                    OnPropertyChanged("Duration");
                }
            }
        }
        private string _description = "";
        [DataMember]
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged("Description");
                }
            }
        }
        private string _properties = "";
        [DataMember]
        public string Properties
        {
            get { return _properties; }
            set
            {
                if (_properties != value)
                {
                    _properties = value;
                    OnPropertyChanged("Properties");
                }
            }
        public event PropertyChangedEventHandler PropertyChanged;
                protected void OnPropertyChanged(string propertyName)
                {
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    

    Add an AppointmentCollection class that inherits from an ObservableCollection:

    C#
    Copy Code
    public class AppointmentCollection : ObservableCollection<Appointment>
    {
        public AppointmentCollection()
        {
        }
    }
    

    And then create an instance of your custom data source on the MainPage.xaml page. Note that you're setting the Key value of your local resource to the name you defined in your code earlier:

    XAML
    Copy Code
    <Page.Resources>
        <local:AppointmentCollection x:Key="_ds"/>
    </Page.Resources>
    

    Use the NestedPropertySetter to set the mapping between the appointment properties set in the Appointment class and the AppointmentStorage class:

    XAML
    Copy Code
    <c1:C1Scheduler x:Name="sched1"  >
        <!-- Map AppointmentStorage to collection of business objects -->
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.AppointmentProperties.MappingName"
            Value="Properties"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.Body.MappingName"
            Value="Description"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.End.MappingName"
            Value="End"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.IdMapping.MappingName"
            Value="Id"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.Location.MappingName"
            Value="Location"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.Start.MappingName"
            Value="Start"/>
        <c1:NestedPropertySetter
            PropertyName="DataStorage.AppointmentStorage.Mappings.Subject.MappingName"
            Value="Subject"/>
        <c1:NestedPropertySetter PropertyName="DataStorage.AppointmentStorage.DataSource"
            Value="{Binding Mode=TwoWay, Source={StaticResource _ds}}" />
    </c1:C1Scheduler>
    

    When you run your application, there will be a test appointment marking the current day.