Thursday, June 18, 2015

Binding ScheduleView to Database part 8: Time Markers

OK guys, we implemented already most of the Appointment functionality, and we have just a little job for completing: Time markers.

ScheduleView provides us a time markers support. we can assign a time marker to each one of our appointments, thus making them easily distinguishable.

Usually, the end user should select from a closed list, and he should not have to edit the list. Therefore, unlike categories. Therefore, Usually we do not need to store the TimeMarker options in the database, and we can set it in the code.

However, if you want, you can also create a Model for TimeMarker, and manage them in the database, as we did with categories. But below, we will implement the easy way.




1. TimeMarkerHelper.cs


public static class TimeMarkerHelper
{
    // static ctor
    static TimeMarkerHelper()
    {
        timeMarkerCollection = new ITimeMarker[] 
        {
            TimeMarker.Busy, 
            TimeMarker.Free,
            TimeMarker.OutOfOffice,
            TimeMarker.Tentative,
            new TimeMarker("Custom",System.Windows.Media.Brushes.LightGray)
        };
    }

    private static readonly ITimeMarker[] timeMarkerCollection;

    public static ITimeMarker[] TimeMarkerCollection
    {
        get { return timeMarkerCollection; }
    }

    public static ITimeMarker ResolveTimeMarkerByName(string timeMarkerName)
    {
        ITimeMarker timeMarker = timeMarkerCollection.FirstOrDefault(tm => tm.TimeMarkerName.Equals(timeMarkerName, StringComparison.CurrentCultureIgnoreCase));
        return timeMarker;
    }
}


2. Changes in AppointmentModelBase

public string TimeMarkerString { get; set; }

ITimeMarker IAppointment.TimeMarker
{
    get;
    set;
}

public ITimeMarker TimeMarker
{
    get { return Helpers.TimeMarkerHelper.ResolveTimeMarkerByName(this.TimeMarkerString); }
    set { this.TimeMarkerString = value.TimeMarkerName; }
}

.....


public virtual void CopyFrom(IAppointment other)
{
    AppointmentModelBase sourceAppointment = other as AppointmentModelBase;

    if (sourceAppointment == null)
        return;

    this.Subject = sourceAppointment.Subject;
    this.Body = sourceAppointment.Body;
    this.Start = sourceAppointment.Start;
    this.End = sourceAppointment.End;
    this.IsAllDayEvent = this.IsAllDayEvent;
    this.Importance = sourceAppointment.Importance;
    this.Category = sourceAppointment.Category;
    this.Resources.Clear();
    this.Resources.AddRange(sourceAppointment.Resources);
    this.TimeMarker = sourceAppointment.TimeMarker;
}

3. Changes in the View

<Window x:Class="DatabaseBindingSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:helpers="clr-namespace:DatabaseBindingSample.Helpers"
        Title="MainWindow" Height="350" Width="525"
        WindowState="Maximized"
        >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <telerik:RadScheduleView AppointmentsSource="{Binding Appointments,Mode=OneTime}"
                                 CategoriesSource="{Binding Categories,Mode=OneTime}"
                                 ResourceTypesSource="{Binding ResourceTypes,Mode=OneTime}"
                                 TimeMarkersSource="{x:Static helpers:TimeMarkerHelper.TimeMarkerCollection}"
                                 VisibleRangeChangedCommand="{Binding LoadAppointmentsCommand}"
                                 VisibleRangeChangedCommandParameter="{Binding VisibleRange, RelativeSource={RelativeSource Self}}"
                                 Grid.Row="0">


That's it! Add-migrations, update-database, and enjoy.



The source code is available on our GitHub repository.

No comments:

Post a Comment