Binding doesn't update for some reason

Hey, i'm having some wierd issues with binding not updating in wpf, here's the relevant code: (the issue is that the item count in the player inventory doesn't update on display) TradeView.xaml (lines 23-48) TradeViewViewModel (lines 49-59) PlayerViewModel InventoryViewModel ItemViewModel ViewModelBase
21 Replies
Nasdack
Nasdack5d ago
You are updating the collection by the indexer which I don't believe is tracked by the binding engine so call OnPropertyChanged explicitly after performing your update
Mąż Zuzanny Harmider Szczęście
ok
private void Trade(TradeViewModel trade)
{
if (trade == null) return;
if (Player.Inventory.Items.ContainsKey(trade.TradeOut.Key))
Player.Inventory.Items[trade.TradeOut.Key].Count = Player.Inventory.Items[trade.TradeOut.Key].Count - trade.TradeOut.Value;
OnPropertyChanged(nameof(Player.Inventory.Items));
if (Player.Inventory.Items.ContainsKey(trade.TradeIn.Key))
Player.Inventory.Items[trade.TradeIn.Key].Count = Player.Inventory.Items[trade.TradeIn.Key].Count - trade.TradeIn.Value;
OnPropertyChanged(nameof(Player.Inventory.Items));
Debug.WriteLine($"{trade.TradeOut.Key}: {Player.Inventory.Items[trade.TradeOut.Key].Count}");
Debug.WriteLine($"{trade.TradeIn.Key}: {Player.Inventory.Items[trade.TradeIn.Key].Count}");
TradeCommand.RaiseCanExecuteChanged();
}
private void Trade(TradeViewModel trade)
{
if (trade == null) return;
if (Player.Inventory.Items.ContainsKey(trade.TradeOut.Key))
Player.Inventory.Items[trade.TradeOut.Key].Count = Player.Inventory.Items[trade.TradeOut.Key].Count - trade.TradeOut.Value;
OnPropertyChanged(nameof(Player.Inventory.Items));
if (Player.Inventory.Items.ContainsKey(trade.TradeIn.Key))
Player.Inventory.Items[trade.TradeIn.Key].Count = Player.Inventory.Items[trade.TradeIn.Key].Count - trade.TradeIn.Value;
OnPropertyChanged(nameof(Player.Inventory.Items));
Debug.WriteLine($"{trade.TradeOut.Key}: {Player.Inventory.Items[trade.TradeOut.Key].Count}");
Debug.WriteLine($"{trade.TradeIn.Key}: {Player.Inventory.Items[trade.TradeIn.Key].Count}");
TradeCommand.RaiseCanExecuteChanged();
}
still nothing @Nasdack i did this the same in GameViewModel and it works fine...
Nasdack
Nasdack5d ago
Can I see your GameView.xaml?
Qwerz
Qwerz5d ago
From what I can tell you're not calling OnPropertyChanged when the value updates
Mąż Zuzanny Harmider Szczęście
it does update tho, and im calling it
Qwerz
Qwerz5d ago
Your model does not implement INotifyPropertyChanged
Mąż Zuzanny Harmider Szczęście
code:
private void Trade(TradeViewModel trade)
{
if (trade == null) return;

var items = PlayerViewModel.Inventory.Items;

if (items.TryGetValue(trade.TradeOut.Key, out var tradeOutItem))
{
tradeOutItem.Count -= trade.TradeOut.Value;
}

if (items.TryGetValue(trade.TradeIn.Key, out var tradeInItem))
{
tradeInItem.Count += trade.TradeIn.Value;
}
Debug.WriteLine($"{trade.TradeOut.Key}: {tradeOutItem?.Count}");
Debug.WriteLine($"{trade.TradeIn.Key}: {tradeInItem?.Count}");
//viewmodelbase
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName=null)
{
Debug.WriteLine($"Property changed: {propertyName}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Trade(TradeViewModel trade)
{
if (trade == null) return;

var items = PlayerViewModel.Inventory.Items;

if (items.TryGetValue(trade.TradeOut.Key, out var tradeOutItem))
{
tradeOutItem.Count -= trade.TradeOut.Value;
}

if (items.TryGetValue(trade.TradeIn.Key, out var tradeInItem))
{
tradeInItem.Count += trade.TradeIn.Value;
}
Debug.WriteLine($"{trade.TradeOut.Key}: {tradeOutItem?.Count}");
Debug.WriteLine($"{trade.TradeIn.Key}: {tradeInItem?.Count}");
//viewmodelbase
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName=null)
{
Debug.WriteLine($"Property changed: {propertyName}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
output:
Property changed: Count
Property changed: Count
Mushroom: -2
Honey: 3
Property changed: Count
Property changed: Count
Mushroom: -2
Honey: 3
it does
Qwerz
Qwerz5d ago
Looking at it, can't see that it does.
Qwerz
Qwerz5d ago
model
Qwerz
Qwerz5d ago
not viewmodel
Mąż Zuzanny Harmider Szczęście
the model shouldn't integrate inotify, it's against mvvm this is all the relevant code
Qwerz
Qwerz5d ago
Now why would that be
Mąż Zuzanny Harmider Szczęście
coz a model is for static stuff i have both model and viewmodel couterparts for everything
Qwerz
Qwerz5d ago
Implementing INPC in a model that holds states is absolutely not against MVVM
Mąż Zuzanny Harmider Szczęście
what model do you want tho, i dont understand like what doesnt that have to do with the binding
Qwerz
Qwerz5d ago
You're trying to update a property of Player
FusedQyou
FusedQyou5d ago
I think you'd do yourself a massive favour if you get rid of all the manual INPC invokation and download a package like CommunityToolkit that does it for you
Mąż Zuzanny Harmider Szczęście
ill try it ig did it, still doesnt work viewmodels @FusedQyou ? i tried even coding it from scratch with the toolkit in mind, still nothing

Did you find this page helpful?