/// <summary> /// Populates the each cell in the sudokuBoard grid with a dynamically created TextBox or Label control. /// Populates text value of Label control using sudokuGame array. Clears children of sudokuBoard grid /// if they already exist. /// /// Referred youtube link - https://www.youtube.com/watch?v=eiGOF0roB18 for dynamic creation of controls /// like label and textbox inside the grid control. /// </summary> /// <param name="sender">New Puzzle button control</param> /// <param name="e">RoutedEventArgs object</param> private void BtnNewPuzzle_Click(object sender, RoutedEventArgs e) { revealFlag = false; ComboBoxItem currentItem = (ComboBoxItem)combodifficultylevel.SelectedItem; difficultyLevel = currentItem.Content.ToString(); sudokuBoard.Children.Clear(); viewModel = new SudokuViewModel(difficultyLevel); int i = 0, j; foreach (var row in sudokuBoard.RowDefinitions) { j = 0; foreach (var col in sudokuBoard.ColumnDefinitions) { Border br = new Border(); Grid.SetRow(br, i); Grid.SetColumn(br, j); br.Background = new SolidColorBrush(Color.FromArgb(100, 100, 100, 100)); if (viewModel.SudokuGame[i, j] == 0) { TextBox textbox = new TextBox { Uid = i + "," + j, Width = 20, Margin = new Thickness(0, 5, 0, 5), MaxLength = 1, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; textbox.PreviewTextInput += ValidateNumber; textbox.PreviewMouseLeftButtonDown += TextBoxClickEvent; br.Child = textbox; } else { Label label = new Label { Content = viewModel.SudokuGame[i, j], Uid = i + "," + j, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; br.Child = label; } br.BorderBrush = Brushes.Black; sudokuBoard.Children.Add(br); j++; } i++; } BeautifySudoku(); sudokuBorder.Visibility = Visibility.Visible; btnSave.Visibility = Visibility.Visible; btnReveal.IsEnabled = true; btnValidate.IsEnabled = true; }
public SudokuControl(SudokuViewModel vm) { InitializeComponent(); DataContext = vm; }
static App() { ViewModelSudoku = new SudokuViewModel(); }