Ashik
Ashik

Reputation: 357

INotifyPropertyChanged in nested object

I have 2 main classes. The first class represents a Cell that can have values X, O or Empty. I have implemented INotifyPropertyChanged on this.

public class Cell : INotifyPropertyChanged
{
    private Symbol state;

    public Symbol State
    {
        get { return state; }
        set
        {
            if (value == Symbol.X || value == Symbol.O)
                state = value;
            OnPropertyChanged("State");
        }
    }

    public Cell()
    {
        state = Symbol.Empty;
    }

    public enum Symbol
    {
        X, O, Empty
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

The second class contains an object of this class and is also set as the datacontext for my main window.

public class Board
{  
    private Cell testCell;

    public Cell TestCell
    {
        get { return testCell; }
        set { testCell = value; }
    }

    public Board()
    {
        TestCell = new Cell();
    }

    public void Cell_Click(int cellNum)
    {
        TestCell.State = Cell.Symbol.O;
    }
}

In my mainwindow I have set the datacontext as board, and also contains a button_click function.

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new Board();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Board board = this.DataContext as Board;
        board.Cell_Click(cellNum);
    }
}

In my XAML I have bound to Cell object within board using a button style like this:

<Setter Property="Background" Value="{Binding TestCell, 
                                      Converter={StaticResource BGConverter}}"/>

BGConverter is a custom converter that accepts a Cell object and converts it into a Colors object. I believe I am indeed directly binding to an object that has INotify implemented, so there's no issue of nested objects. However the binding doesn't reflect changes when I click. When debugging, I found that PropertyChanged event is always null.

The closest answer I found for this is that the event will be subscribed to only if the class Cell is my datacontext. Atleast that's what I understood. How can I correct this problem?

Also I am fresh out of college, currently learning WPF on a new job, so any general recommendations are welcome too. Thanks

Upvotes: 0

Views: 1307

Answers (2)

Ashik
Ashik

Reputation: 357

Simply bind to TestCell.State instead of TestCell

Upvotes: 2

Ash Saf
Ash Saf

Reputation: 162

I'm pretty new at this myself, but I believe your data context needs to implement INotifyPropertyChanged as well. That is, your Board class needs to listen to the PropertyChanged event of the cells and fire its own PropertyChanged event when this happens.

Upvotes: 0

Related Questions