// Public Constructor public ColorGrid() { // Creation Border obj for element bord = new Border(); bord.BorderBrush = SystemColors.ControlDarkDarkBrush; bord.BorderThickness = new Thickness(1); AddVisualChild(bord); AddLogicalChild(bord); // Creation UniformGrid as child of Border unigrid = new UniformGrid(); unigrid.Background = SystemColors.WindowBrush; unigrid.Columns = xNum; bord.Child = unigrid; // Filling panel UniformGrid with ColorCell objects for (int y = 0; y < yNum; y++) { for (int x = 0; x < xNum; x++) { Color clr = (Color)typeof(Colors). GetProperty(strColors[y, x]).GetValue(null, null); cells[y, x] = new ColorCell(clr); unigrid.Children.Add(cells[y, x]); if (clr == SelectedColor) { cellSelected = cells[y, x]; cells[y, x].IsSelected = true; } ToolTip tip = new ToolTip(); tip.Content = strColors[y, x]; cells[y, x].ToolTip = tip; } } }
protected override void OnMouseLeave(MouseEventArgs args) { base.OnMouseLeave(args); if (cellHighlighted != null) { cellHighlighted.IsHighlighted = false; cellHighlighted = null; } }
protected override void OnLostKeyboardFocus( KeyboardFocusChangedEventArgs args) { base.OnGotKeyboardFocus(args); //base.OnLostKeyboardFocus(args); if (cellHighlighted != null) { cellHighlighted.IsHighlighted = false; cellHighlighted = null; } }
protected override void OnMouseMove(MouseEventArgs args) { base.OnMouseMove(args); ColorCell cell = args.Source as ColorCell; if (cell != null) { if (cellHighlighted != null) { cellHighlighted.IsHighlighted = false; } cellHighlighted = cell; cellHighlighted.IsHighlighted = true; } }
protected override void OnMouseDown(MouseButtonEventArgs args) { base.OnMouseDown(args); ColorCell cell = args.Source as ColorCell; if (cell != null) { if (cellHighlighted != null) { cellHighlighted.IsSelected = false; } cellHighlighted = cell; cellHighlighted.IsSelected = true; } Focus(); }
// Handling keyboard events protected override void OnGotKeyboardFocus( KeyboardFocusChangedEventArgs args) { base.OnGotKeyboardFocus(args); if (cellHighlighted == null) { if (cellHighlighted != null) { cellHighlighted = cellSelected; } else { cellHighlighted = cells[0, 0]; } cellHighlighted.IsHighlighted = true; } }
protected override void OnMouseUp(MouseButtonEventArgs args) { base.OnMouseUp(args); ColorCell cell = args.Source as ColorCell; if (cell != null) { if (cellSelected != null) { cellSelected.IsSelected = false; } cellSelected = cell; cellSelected.IsSelected = true; clrSelected = (cellSelected.Brush as SolidColorBrush).Color; OnSelectedColorChanged(EventArgs.Empty); } }
protected override void OnKeyDown(KeyEventArgs args) { base.OnKeyDown(args); int index = unigrid.Children.IndexOf(cellHighlighted); int y = index / xNum; int x = index % xNum; switch (args.Key) { case Key.Home: y = 0; x = 0; break; case Key.End: y = yNum - 1; x = xNum + 1; break; case Key.Down: if ((y = (y + 1) % yNum) == 0) { x++; } break; case Key.Up: if ((y = (y + yNum - 1) % yNum) == yNum - 1) { x--; } break; case Key.Right: if ((x = (x + 1) % xNum) == 0) { y++; } break; case Key.Left: if ((x = (x + xNum - 1) % xNum) == xNum - 1) { y--; } break; case Key.Enter: case Key.Space: if (cellSelected != null) { cellSelected.IsSelected = false; } cellSelected = cellHighlighted; cellSelected.IsSelected = true; clrSelected = (cellSelected.Brush as SolidColorBrush).Color; OnSelectedColorChanged(EventArgs.Empty); break; default: return; } if (x >= xNum || y >= yNum) { MoveFocus(new TraversalRequest( FocusNavigationDirection.Previous)); } else { cellHighlighted.IsHighlighted = false; cellHighlighted = cells[y, x]; cellHighlighted.IsHighlighted = true; } args.Handled = true; }