Back to Devexpress

How to: Create Custom Labels and Statuses

wpf-116501-controls-and-libraries-scheduler-examples-how-to-create-custom-labels-and-statuses.md

latest10.7 KB
Original Source

How to: Create Custom Labels and Statuses

  • Apr 01, 2021
  • 5 minutes to read

This example demonstrates how to customize the appointment marks - labels and statuses used to identify appointments in the Scheduler. The default label and status items are similar to the corresponding marks in Microsoft® Outlook®. However, you can create your own labels and statuses, as described below.

This example consists of the following sections:

Create a New Application

  1. Create a new WPF Application project and open the MainWindow.xaml file in the Visual Studio Designer.

  2. Add the SchedulerControl object to your project. You can do this by dragging the SchedulerControl item from the DX.25.2: Scheduling Toolbox tab to the canvas.

  3. Right-click the SchedulerControl object and select Layout | Reset All in the context menu to stretch the SchedulerControl so that it fills the entire window.

Create a ViewModel with a Data Source

This example uses the DevExpress MVVM Framework to create a POCO ViewModel that provides data for the application. The CustomLabel and PaymentState class instances represent the custom labels and custom statuses, respectively.

View Example

csharp
using DevExpress.Mvvm.POCO;
using System.Windows.Media;
    public class CustomLabel {
        public static CustomLabel Create() {
            return ViewModelSource.Create(() => new CustomLabel());
        }
        protected CustomLabel() { }
        public virtual int Id { get; set; }
        public virtual string Caption { get; set; }
        public virtual Color Color { get; set; }
    }
vb
Imports DevExpress.Mvvm.POCO
Imports System.Windows.Media
    Public Class CustomLabel
        Public Shared Function Create() As CustomLabel
            Return ViewModelSource.Create(Function() New CustomLabel())
        End Function
        Protected Sub New()
        End Sub
        Public Overridable Property Id() As Integer
        Public Overridable Property Caption() As String
        Public Overridable Property Color() As Color
    End Class

View Example

csharp
using DevExpress.Mvvm.POCO;
using System.Windows.Media;
    public class PaymentState {
        public static PaymentState Create() {
            return ViewModelSource.Create(() => new PaymentState());
        }

        protected PaymentState() { }
        public virtual int Id { get; set; }
        public virtual string Caption { get; set; }
        public virtual Brush Brush { get; set; }
    }
vb
Imports DevExpress.Mvvm.POCO
Imports System.Windows.Media
    Public Class PaymentState
        Public Shared Function Create() As PaymentState
            Return ViewModelSource.Create(Function() New PaymentState())
        End Function

        Protected Sub New()
        End Sub
        Public Overridable Property Id() As Integer
        Public Overridable Property Caption() As String
        Public Overridable Property Brush() As Brush
    End Class

Add the MainViewModel class to your project. Create two observable collections within the class that store custom labels and statuses, and are used for data binding.

Note

This code snippet also contains the Doctors and Appointments observable collections, which are used to populate the scheduler application with sample data. Refer to the Getting Started topic for more details.

View Example

csharp
public virtual ObservableCollection<Doctor> Doctors { get; set; }
public virtual ObservableCollection<MedicalAppointment> Appointments { get; set; }
public virtual ObservableCollection<PaymentState> Statuses { get; set; }
public virtual ObservableCollection<CustomLabel> Labels { get; set; }

public static string[] AppointmentTypes = { "Hospital", "Office", "Phone Consultation", "Home", "Hospice" };
public static Color[] AppointmentColorTypes = { Color.FromRgb(168, 213, 255), Color.FromRgb(255, 194, 190),
    Color.FromRgb(255, 247, 165), Color.FromRgb(193, 244, 156), Color.FromRgb(244, 206, 147) };

public static string[] PaymentStates = { "Paid", "Unpaid" };
public static Brush[] PaymentBrushStates = { new LinearGradientBrush(Colors.Green,Colors.Yellow, 45.0), new SolidColorBrush(Colors.Red) };

Random rand = new Random(DateTime.Now.Millisecond);

protected MainViewModel() {
    Statuses = CreateStatuses();
    Labels = CreateLabels();
    Doctors = CreateDoctors();
    Appointments = CreateMedicalAppointments();
}

ObservableCollection<CustomLabel> CreateLabels() {
    ObservableCollection<CustomLabel> result = new ObservableCollection<CustomLabel>();
    int count = AppointmentTypes.Length;
    for (int i = 0; i < count; i++) {
        CustomLabel label = CustomLabel.Create();
        label.Id = i;
        label.Color = AppointmentColorTypes[i];
        label.Caption = AppointmentTypes[i];
        result.Add(label);
    }
    return result;
}

ObservableCollection<PaymentState> CreateStatuses() {
    ObservableCollection<PaymentState> result = new ObservableCollection<PaymentState>();
    int count = PaymentStates.Length;
    for (int i = 0; i < count; i++) {
        PaymentState paymentState = PaymentState.Create();
        paymentState.Id = i;
        paymentState.Brush = PaymentBrushStates[i];
        paymentState.Caption = PaymentStates[i];
        result.Add(paymentState);
    }
    return result;
}
vb
Public Overridable Property Doctors() As ObservableCollection(Of Doctor)
Public Overridable Property Appointments() As ObservableCollection(Of MedicalAppointment)
Public Overridable Property Statuses() As ObservableCollection(Of PaymentState)
Public Overridable Property Labels() As ObservableCollection(Of CustomLabel)

Public Shared AppointmentTypes() As String = { "Hospital", "Office", "Phone Consultation", "Home", "Hospice" }
Public Shared AppointmentColorTypes() As Color = { Color.FromRgb(168, 213, 255), Color.FromRgb(255, 194, 190), Color.FromRgb(255, 247, 165), Color.FromRgb(193, 244, 156), Color.FromRgb(244, 206, 147) }

Public Shared PaymentStates() As String = { "Paid", "Unpaid" }
Public Shared PaymentBrushStates() As Brush = { _
    New LinearGradientBrush(Colors.Green,Colors.Yellow, 45.0), _
    New SolidColorBrush(Colors.Red) _
}

Private rand As New Random(Date.Now.Millisecond)

Protected Sub New()
    Statuses = CreateStatuses()
    Labels = CreateLabels()
    Doctors = CreateDoctors()
    Appointments = CreateMedicalAppointments()
End Sub

Private Function CreateLabels() As ObservableCollection(Of CustomLabel)
    Dim result As New ObservableCollection(Of CustomLabel)()
    Dim count As Integer = AppointmentTypes.Length
    For i As Integer = 0 To count - 1
        Dim label As CustomLabel = CustomLabel.Create()
        label.Id = i
        label.Color = AppointmentColorTypes(i)
        label.Caption = AppointmentTypes(i)
        result.Add(label)
    Next i
    Return result
End Function

Private Function CreateStatuses() As ObservableCollection(Of PaymentState)
    Dim result As New ObservableCollection(Of PaymentState)()
    Dim count As Integer = PaymentStates.Length
    For i As Integer = 0 To count - 1

        Dim paymentState_Renamed As PaymentState = CustomLabelsAndStatusesExample.PaymentState.Create()
        paymentState_Renamed.Id = i
        paymentState_Renamed.Brush = PaymentBrushStates(i)
        paymentState_Renamed.Caption = PaymentStates(i)
        result.Add(paymentState_Renamed)
    Next i
    Return result
End Function

Specify the MainViewModel instance as the data context for data binding by assigning it to the FrameworkElement.DataContext property.

xaml
<Window x:Class="CustomLabelsAndStatusesExample.MainWindow"
        Title="MainWindow"
        DataContext="{dxmvvm:ViewModelSource local:MainViewModel}"
        ...>

Specify Mappings for Label and Status Properties

Bind the specified data sources containing custom labels and statuses to the scheduler’s DataSource using the DataSource.AppointmentLabelsSource and DataSource.AppointmentStatusesSource properties.

Use the DataSource.AppointmentLabelMappings and DataSource.AppointmentStatusMappings properties to map label and status properties to the CustomLabel and PaymentState class properties, respectively.

View Example

xaml
<dxsch:SchedulerControl x:Name="scheduler">
    <dxsch:DayView ShowWorkTimeOnly="True"/>
    <dxsch:SchedulerControl.DataSource>
        <dxsch:DataSource AppointmentsSource="{Binding Appointments}" ResourcesSource="{Binding Doctors}" 
                          AppointmentLabelsSource="{Binding Labels}" AppointmentStatusesSource="{Binding Statuses}">
            <dxsch:DataSource.AppointmentLabelMappings>
                <dxsch:AppointmentLabelMappings
                    Color="Color"
                    Caption="Caption"
                    Id="Id" />
            </dxsch:DataSource.AppointmentLabelMappings>
            <dxsch:DataSource.AppointmentStatusMappings>
                <dxsch:AppointmentStatusMappings
                    Brush="Brush"
                    Caption="Caption"
                    Id="Id" />
            </dxsch:DataSource.AppointmentStatusMappings>

Result

Run the application. The following image illustrates the result: