public TimeRestrictionsControl()
 {
     InitializeComponent();
     for (var day = 0; day < DaysPerWeek; day++)
     {
         for (var hour = 0; hour < HoursPerDay; hour++)
         {
             _hourCells[day,hour] = new ToggleCell();
             Grid.SetColumn(_hourCells[day,hour], hour + 1);
             Grid.SetRow(_hourCells[day,hour], day + 1);
             _hourCells[day, hour].PreviewMouseLeftButtonDown += TimeGrid_PreviewMouseLeftButtonDown;
             _hourCells[day, hour].PreviewMouseLeftButtonUp += TimeGrid_PreviewMouseLeftButtonUp;
             _hourCells[day, hour].PreviewMouseMove += TimeGrid_PreviewMouseMove;
             WeekGrid.Children.Add(_hourCells[day, hour]);
         }
     }
     WeekGrid.DataContext = this;
 }
 void EndDragging()
 {
     _mouseDragStart = null;
     _dragSource = null;
     _dragRegion = null;
 }
 void ApplyActionToRegion(ToggleCell[,] cells, Action<ToggleCell> action)
 {
     var startx = Grid.GetColumn(TopLeft) - 1;
     var endx = Grid.GetColumn(BottomRight) - 1;
     var starty = Grid.GetRow(TopLeft) - 1;
     var endy = Grid.GetRow(BottomRight) - 1;
     for (var x = startx; x <= endx; x++)
     {
         for (var y = starty; y <= endy; y++)
         {
             action(cells[y, x]);
         }
     }
 }
 public void SetVisualStates(ToggleCell[,] cells, string state)
 {
     ApplyActionToRegion(cells, (cell) => VisualStateManager.GoToState(cell, state, true));
 }
 public void ApplySelection(ToggleCell[,] cells, bool select)
 {
     ApplyActionToRegion(cells, (cell) =>
     {
         VisualStateManager.GoToState(cell, "NoDrag", true);
         VisualStateManager.GoToState(cell, "Normal", true);
         cell.Selected = select;
     });
 }
 public CellRegion(ToggleCell[,] cells, ToggleCell first, ToggleCell second)
 {
     var firstx = Grid.GetColumn(first) - 1;
     var secondx = Grid.GetColumn(second) - 1;
     var firsty = Grid.GetRow(first) - 1;
     var secondy = Grid.GetRow(second) - 1;
     var minx = Math.Min(firstx, secondx);
     var miny = Math.Min(firsty, secondy);
     var maxx = Math.Max(firstx, secondx);
     var maxy = Math.Max(firsty, secondy);
     TopLeft = cells[miny, minx];
     BottomRight = cells[maxy, maxx];
 }
 void TimeGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     var element = sender as ToggleCell;
     if (element == null) return;
     _dragSource = element;
     _dragSource.Selected = !_dragSource.Selected;
     _mouseDragStart = e.GetPosition(this);
 }