private void BuildDynamicColorGrid() { // DataContext is being set in XAML. FlatUIColorPickerViewModel flatUIColorPickerViewModel = (FlatUIColorPickerViewModel)DataContext; int numberOfRows = GetNumberOfRows(flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count); int numberOfColumns = Convert.ToInt32(Math.Ceiling((decimal)flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count / numberOfRows)); Grid flatColorGrid = new Grid(); for (int i = 0; i < numberOfRows; i++) { flatColorGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); } for (int i = 0; i < numberOfColumns; i++) { flatColorGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); } int flatColorIndex = 0; for (int row = 0; row < numberOfRows; row++) { for (int column = 0; column < numberOfColumns; column++) { if (flatColorIndex < flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count) { string colorHex = flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors[flatColorIndex].Hex; Button colorButton = new Button { Background = (SolidColorBrush)(new BrushConverter().ConvertFrom(colorHex)), Command = flatUIColorPickerViewModel.ColorClickCommand, CommandParameter = colorHex }; Grid.SetRow(colorButton, row); Grid.SetColumn(colorButton, column); flatColorGrid.Children.Add(colorButton); flatColorIndex++; } } } // "MainGrid" is set in the name attribute of the root grid in XAML. // We want to add this dynamic grid to the second row of the root grid so it can take up all the remaining space available. Grid.SetRow(flatColorGrid, 1); MainGrid.Children.Add(flatColorGrid); }
private void BuildDynamicColorGrid() { FlatUIColorPickerViewModel flatUIColorPickerViewModel = (FlatUIColorPickerViewModel)DataContext; int numberOfRows = GetNumberOfRows(flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count); int numberOfColumns = Convert.ToInt32(Math.Ceiling((decimal)flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count / numberOfRows)); Grid flatColorGrid = new Grid(); for (int i = 0; i < numberOfRows; i++) { flatColorGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); } for (int i = 0; i < numberOfColumns; i++) { flatColorGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); } int flatColorIndex = 0; for (int row = 0; row < numberOfRows; row++) { for (int column = 0; column < numberOfColumns; column++) { if (flatColorIndex < flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors.Count) { string colorHex = flatUIColorPickerViewModel.FlatUIColorPicker.FlatColors[flatColorIndex].Hex; // Using Rectangle instead of Button because in UWP the hardcoded Button OnHover changes make this tool difficult to use. Windows.UI.Xaml.Shapes.Rectangle colorTile = new Windows.UI.Xaml.Shapes.Rectangle { Fill = PlatformShim.BrushConverterConvertFrom(colorHex), Margin = new Thickness(1, 1, 1, 1), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Tag = colorHex, Stroke = Application.Current.Resources["ButtonBorderThemeBrush"] as Windows.UI.Xaml.Media.Brush, StrokeThickness = string.Equals(ViewModelLocator.Current.FlatUIColorPickerViewModel.SelectedHex, colorHex) ? SELECTED_TILE_STROKE_THICKNESS : 0D }; colorTile.PointerPressed += new PointerEventHandler(ColorTile_OnClick); Grid.SetRow(colorTile, row); Grid.SetColumn(colorTile, column); flatColorGrid.Children.Add(colorTile); flatColorIndex++; } } } // "MainGrid" is set in the name attribute of the root grid in XAML. // We want to add this dynamic grid to the second row of the root grid so it can take up all the remaining space available. Grid.SetRow(flatColorGrid, 1); MainGrid.Children.Add(flatColorGrid); }