Skip to content

Instantly share code, notes, and snippets.

@arthurrump
Created September 12, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurrump/3ff3d03c403f0f1ef9f9a0b15085decc to your computer and use it in GitHub Desktop.
Save arthurrump/3ff3d03c403f0f1ef9f9a0b15085decc to your computer and use it in GitHub Desktop.
DayPanel prototype
<UserControl
x:Class="ScheduleControlProto.AppointmentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ScheduleControlProto"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="MyGrid">
<!-- Nothing here yet, just a placeholder -->
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
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;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace ScheduleControlProto
{
public sealed partial class AppointmentControl : UserControl, IDateRange
{
public AppointmentControl(Color color, string a)
{
this.InitializeComponent();
MyGrid.Background = new SolidColorBrush(color);
A = a;
}
public string A { get; set; }
public DateTimeOffset Start
{
get { return (DateTimeOffset)GetValue(StartProperty); }
set { SetValue(StartProperty, value); }
}
// Using a DependencyProperty as the backing store for Start. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StartProperty =
DependencyProperty.Register("Start", typeof(DateTimeOffset), typeof(AppointmentControl), new PropertyMetadata(default(DateTimeOffset)));
public DateTimeOffset End
{
get { return (DateTimeOffset)GetValue(EndProperty); }
set { SetValue(EndProperty, value); }
}
// Using a DependencyProperty as the backing store for End. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EndProperty =
DependencyProperty.Register("End", typeof(DateTimeOffset), typeof(AppointmentControl), new PropertyMetadata(default(DateTimeOffset)));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScheduleControlProto
{
static class DateRange
{
public static List<List<T>> Overlaps<T>(IEnumerable<T> ranges) where T : IDateRange
{
ranges = ranges.OrderBy(x => x.End - x.Start);
List<List<T>> result = new List<List<T>>();
foreach (T r in ranges)
{
if (!result.SelectMany(x => x).Contains(r))
result.Add(ranges.Where(x => Overlaps(r, x)).ToList());
}
return result;
}
public static bool Overlaps(IDateRange range1, IDateRange range2)
{
return (range1.Start < range2.End && range1.End > range2.Start);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScheduleControlProto
{
interface IDateRange
{
DateTimeOffset Start { get; set; }
DateTimeOffset End { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
namespace ScheduleControlProto
{
class ScheduleDayPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
=> availableSize;
protected override Size ArrangeOverride(Size finalSize)
{
IEnumerable<AppointmentControl> children = Children.Select(x => x as AppointmentControl);
List<List<AppointmentControl>> overlapsList = DateRange.Overlaps(children);
List<AppointmentControl> done = new List<AppointmentControl>();
foreach (var overlaps in overlapsList)
{
double width = finalSize.Width / overlaps.Count;
double xpos = 0;
foreach (var a in overlaps)
{
if (done.Contains(a))
continue;
var point = new Point(xpos, GetYPos(a, finalSize));
var size = new Size(width, GetHeight(a, finalSize));
a.Arrange(new Rect(point, size));
xpos += width;
done.Add(a);
}
}
return finalSize;
}
private double GetHeight(AppointmentControl appointment, Size availableSize)
{
return ((double)(appointment.End - appointment.Start).Ticks / TimeSpan.TicksPerDay) * availableSize.Height;
}
private double GetYPos(AppointmentControl appointment, Size availableSize)
{
return ((double)appointment.Start.TimeOfDay.Ticks / TimeSpan.TicksPerDay) * availableSize.Height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment