Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(ViewModel)DataContext returning null

Tags:

c#

wpf

I've got the following event:

private void PlaceToken(object sender, RoutedEventArgs e)
{
    ConnectFourViewModel currentViewModel = (ConnectFourViewModel)DataContext;

    Button button = (Button)sender;

    int currentColumn = Convert.ToInt32(button.Content);

    int currentPlayer = currentViewModel.Board.CurrentPlayer;
    currentViewModel.Board.PlaceToken(currentColumn, currentPlayer);
}

After the following set up:

public MainWindow()
{
    var window = new Window();
    var grid = new Grid {};
    ConnectFourViewModel ViewModel = new ConnectFourViewModel();

    //Set up rows and cols
    for(int i = 1; i<=7; i++)
    {
        var col = new ColumnDefinition();
        grid.ColumnDefinitions.Add(col);
    }
    for (int i = 1; i <= 7; i++)
    {
        var row = new RowDefinition();
        grid.RowDefinitions.Add(row);
    }

    //Set up tiles
    foreach (var item in ViewModel.Board.AllTiles)
    {
        int index = ViewModel.Board.AllTiles.IndexOf(item);

        string name =
              "Col" +
              Convert.ToString(item.Column) +
              "_Row" +
              Convert.ToString(item.Row);

        Label currentTile = new Label{ Name = name};
        Grid.SetRow(currentTile, item.Row - 1);
        Grid.SetColumn(currentTile, item.Column -1);

        //Bind
        var binding = new Binding();
        binding.Source = ViewModel.Board.AllTiles[index];
        binding.Path = new PropertyPath("Contents");

        currentTile.SetBinding(ContentProperty, binding);

        //Add
        grid.Children.Add(currentTile);
    }

    //Set up Buttons
    for (int i = 1; i <= 7; i++)
    {
        Button currentButton = new Button { };

        //binding
        var binding = new Binding();
        binding.Source = ViewModel.CurrentColumn;

        currentButton.SetBinding(ContentProperty, binding);

        //Set Column names, this has to be after the binding has been set.
        currentButton.Content = i;
        //events
        currentButton.Click += new RoutedEventHandler(PlaceToken);

        //add
        Grid.SetColumn(currentButton, i - 1);
        Grid.SetRow(currentButton, 7);
        grid.Children.Add(currentButton);
    }

    window.Content = grid;
    window.DataContext = ViewModel;
    window.Show();
    InitializeComponent();
}

I'm expecting the line ConnectFourViewModel currentViewModel = (ConnectFourViewModel)DataContext; to set currentViewModel to reflect the information my UI is running off. Unfortunately it's returning null and I'm not sure why.

This is clearly highlighting a gap in my understanding of the topic, but unsure on how to tackle it I could use a hand. Any idea where I've gone wrong?

like image 289
MGDavies Avatar asked Jun 12 '26 13:06

MGDavies


1 Answers

You haven't assigned the DataContext to ViewModel anywhere. Assuming you want the ViewModel to be the ViewModel of MainWindow, I suggest you assigne it before Initialize Component as shown below. Since it's never assigned you are getting null.

public MainWindow()
{
    ConnectFourViewModel ViewModel = new ConnectFourViewModel();
    . . .
    . . .
    DataContext = ViewModel ;
    InitializeComponent();
}
like image 193
Carbine Avatar answered Jun 15 '26 04:06

Carbine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!