0

I've been creating a Sudoku Solver in C# using Wpf as View. In the Constructor of the MainViewModel I have this code.

public MainViewModel()
{
    SudokuTable = new char[9][];
    for (int i = 0; i < 9; i++)
    {
        SudokuTable[i] = new char[9]
          { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
     }
     Solve = new RelayCommand(o => SudokuStart());
     Reset = new RelayCommand(o => SudokuReset());
}

The RelayCommands are linked to Commands of 2 Buttons inside my View. The SudokuTable is bound to an Grid of Textboxes. Each Textbox is linked to a different jagged array Element. Example:

 <TextBox Grid.Row = "0" Grid.Column = "0" Style = "{StaticResource GridTextBox}" Text = "{Binding Path=SudokuTable[0][0]}" />

As people might want to solve multiple Sudokus, I implemented a "Reset" Button. Once it's clicked it does this:

private void SudokuReset()
{
    SudokuTable = new char[9][];
    for (int i = 0; i < 9; i++)
    {
        SudokuTable[i] = new char[9] 
          { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
     }
 }

So basically it does everything it would when starting to Application. But for some reason all textboxes are empty as soon as I click on the Reset-Button. They don't contain ' ' which causes a NullReferenceException. Can anyone tell me the difference between the first Initialization and the reset of it? What am I doing wrong?

I'm thankful for all answers.

Edit as someone asked about my Property:

        private char[][] sudokuTable;
        public char[][] SudokuTable
        {
            get { return sudokuTable; }
            set
            {
                if (sudokuTable != value)
                {
                    sudokuTable = value;
                    OnPropertyChanged("SudokuTable");
                }
            }
        }
2
  • You should probably check the Designer.cs file and see what C# code is being generated from your designer. I suspect that the binding doesn't do what you think it does. Or it will need an updatetrigger because the char you are referencing isn't doing any notifypropertychange stuff. Commented May 13, 2020 at 9:05
  • @Tormod Edited for clarification. Commented May 13, 2020 at 9:09

1 Answer 1

1

This is because the PropertyChanged event is triggered in the line SudokuTable = new char[9][]; Everything you do with the array afterwards is not noticed by the UI / binding.

If you first initialize the jagged array and then set it, it should behave as you expect.

var newTable = new char[9][];
for (int i = 0; i < 9; i++)
{
    newTable[i] = new char[9]
      { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
}

SudokuTable = newTable;

Why does it behave different to the constructor?

This is because your constructor is executed and then your view model gets set as the BindingContext. And changing a binding context triggers reevaluation of the bindings.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I actually didn't know this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.