If I add something to my collection, the UI is updated, so my bindings are in order. If I make an update to the collection, the UI is not updated until I navigate away from the page and back to it.
My collection definition:
public ObservableCollection<Account> Accounts = new();
Account
definition:
public class Account
{
public int Id { get; set; }
public string AccountName { get; set; }
public decimal StartingBalance { get; set; }
public override string ToString()
{
return $"Id: {Id}: {AccountName} | {StartingBalance}";
}
}
I make updates like this:
public void EditAccount(string editAccountName, string editAccountStartingBalance, int editAccountId)
{
decimal startingBalance;
Decimal.TryParse(editAccountStartingBalance, out startingBalance);
Account editAccount = Accounts.FirstOrDefault(x => x.Id == editAccountId);
editAccount.AccountName = editAccountName;
editAccount.StartingBalance = startingBalance;
}
As I typed up my question, I found through other questions that I need to do the following:
private ObservableCollection<Account> _accounts = new();
public ObservableCollection<Account> Accounts
{
get { return _accounts; }
set
{
_accounts = value;
RaisePropertyChanged("Accounts");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
This isn't updating the UI either. I can see via stepping through the code that the collection itself is updated, but it's still not reflected in the UI until I navigate away from the page then back.
Code snippets have been pared down for brevity. I think I included all the needed deets. :) With so many things automagically working in .NET MAUI, I think I'm expecting too much, lol.