private void SetupTextboxes(Sudoku.ViewModels.MainViewModel viewModel) { Board board = viewModel.Board; var rangeRule = new ValidationRules.BoardRange(board); for (int row = 1; row <= board.Size; row++) { var gridRow = row + (row / board.SquareSize) - (row % board.SquareSize == 0 ? 1 : 0); for (int column = 1; column <= board.Size; column++) { var gridColumn = column + (column / board.SquareSize) - (column % board.SquareSize == 0 ? 1 : 0); var textbox = new TextBox { Height = 40, Width = 40, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, FontSize = 20 }; Grid.SetColumn(textbox, gridColumn); Grid.SetRow(textbox, gridRow); AssignShortcut(textbox, row, column); var cell = board[row, column]; textbox.DataContext = cell; textbox.PreviewLostKeyboardFocus += (sender, e) => { var tb = sender as TextBox; int value; if (!string.IsNullOrEmpty(tb.Text) && !(int.TryParse(tb.Text, out value) && cell.IsValid(value))) { e.Handled = true; } else { tb.ToolTip = null; } }; textbox.IsEnabled = cell.Enabled; if (!cell.Enabled) { textbox.Foreground = disbleBrush; } Children.Add(textbox); var textbinding = new Binding { Mode = BindingMode.TwoWay, Path = new PropertyPath("Value"), TargetNullValue = string.Empty, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, //ValidatesOnDataErrors = true, //NotifyOnValidationError = true, NotifyOnTargetUpdated = true, UpdateSourceExceptionFilter = (expression, exception) => { viewModel.Notify(exception.Message); textbox.ToolTip = exception.Message; return(expression); } }; textbinding.ValidationRules.Add(rangeRule); textbox.SetBinding(TextBox.TextProperty, textbinding); } } AssignShortcutsToWindow(); }
private void SetupTextboxes(Sudoku.ViewModels.MainViewModel viewModel) { Board board = viewModel.Board; var rangeRule = new ValidationRules.BoardRange(board); for (int row = 1; row <= board.Size; row++) { var gridRow = row + (row / board.SquareSize) - (row % board.SquareSize == 0 ? 1 : 0); for (int column = 1; column <= board.Size; column++) { var gridColumn = column + (column / board.SquareSize) - (column % board.SquareSize == 0 ? 1 : 0); var textbox = new TextBox { Height = 40, Width = 40, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, FontSize = 20 }; Grid.SetColumn(textbox, gridColumn); Grid.SetRow(textbox, gridRow); AssignShortcut(textbox, row, column); var cell = board[row, column]; textbox.DataContext = cell; textbox.PreviewLostKeyboardFocus += (sender, e) => { var tb = sender as TextBox; int value; if (!string.IsNullOrEmpty(tb.Text) && !(int.TryParse(tb.Text, out value) && cell.IsValid(value))) e.Handled = true; else tb.ToolTip = null; }; textbox.IsEnabled = cell.Enabled; if (!cell.Enabled) textbox.Foreground = disbleBrush; Children.Add(textbox); var textbinding = new Binding { Mode = BindingMode.TwoWay, Path = new PropertyPath("Value"), TargetNullValue = string.Empty, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, //ValidatesOnDataErrors = true, //NotifyOnValidationError = true, NotifyOnTargetUpdated = true, UpdateSourceExceptionFilter = (expression, exception) => { viewModel.Notify(exception.Message); textbox.ToolTip = exception.Message; return expression; } }; textbinding.ValidationRules.Add(rangeRule); textbox.SetBinding(TextBox.TextProperty, textbinding); } } AssignShortcutsToWindow(); }