I have a WinForms project that uses data bindings to track realtime prices. The prices are bound like so:
var btcPriceInUsdBinding = new Binding("Text", Client, "BtcPriceInUsd", true, DataSourceUpdateMode.OnPropertyChanged);
btcPriceInUsdBinding.Format += (s, e) => { e.Value = string.Format("BTC/USD: ${0:#####0.00}", e.Value); };
btcUsdValueLabel.DataBindings.Add(btcPriceInUsdBinding);
The actual object storing the data inherits from an ObservableObject
- that is, an abstract base class that implements INotifyPropertyChanged.
public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify([CallerMemberName]string property = null)
{
if (property != null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
Then when a value is changed, it is fired like this
public decimal BtcPriceInUsd
{
get { return _btcPriceInUSD; }
set {
_btcPriceInUSD = value;
Notify();
}
}
Debugging, I can see that the PropertyChanged is correctly being called, and breaking inside of the Binding.Format
event shows that e.Value
is the correct value. However, despite all of this, the label does not update. Why is this happening?