Hello,
You change the State
by Items.ElementAt(row * Columns + col).State = TetrisState.Locked;
, but you set the BindingContext
by boxView.BindingContext = itemsEnumerator.Current;
. there are different DataSource. So, you cannot change the background color at the runtime.
Firstly, we can create ObservableCollection<TetrisBoxCellModel>
//I create 3 Rows and 3 Columns Gird.
int Rows=3;
int Columns=3;
ObservableCollection<TetrisBoxCellModel> Items = new System.Collections.ObjectModel.ObservableCollection<TetrisBoxCellModel>();
//I add 9 records to Items for testing, you can add remaining 6 records.
Items.Add(new TetrisBoxCellModel() { State = TetrisState.Released });
Items.Add(new TetrisBoxCellModel() { State = TetrisState.Released });
Items.Add(new TetrisBoxCellModel() { State = TetrisState.Used });
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
{
var boxView = new BoxView();
//set the binding with current index.
boxView.BindingContext = Items[i * Columns + j];
var backgroundColorBinding = new Binding("State", BindingMode.TwoWay, new CellStateToBackgroundColorConverter());
boxView.SetBinding(VisualElement.BackgroundColorProperty,
backgroundColorBinding);
control.Children.Add(boxView, i, j);
}
}
When you need to change the value of the State
in the ExecuteStartCommand
method, you can set it with Items[row * Columns + col].State = TetrisState.Locked;
You can also try to use this XAML data binding diagnostics - Visual Studio (Windows) tools in Visual Studio to detect and resolve data binding errors in XAML projects.
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.