All articles

Best Practices: How to implement INotifyPropertyChanged right?

Published 2010-06-17 - Last modified 2010-06-18

Original: https://soci.hu/blog/index.php/2010/06/17/best-practices-how-to-implement-inotifypropertychanged-right/

Jó összefoglaló a témában.

Update: ez lett a vége:

using System.ComponentModel;
using System.Threading;
using Krs.Ats.IBNet;

namespace ATS.IB
{
///

/// Real-time data descriptor for IB tick data
///

public class MarketData : INotifyPropertyChanged
{
public Contract IBContract { get; set; }
public Ticker Ticker { get; set; }

private decimal lastPrice;
public decimal LastPrice
{
get { return lastPrice; }
set
{
lastPrice = value;
OnPropertyChanged(LastPriceChangedArgs);
}
}

private decimal lastBid;
public decimal LastBid
{
get { return lastBid; }
set
{
lastBid = value;
OnPropertyChanged(LastBidChangedArgs);
}
}

private decimal lastAsk;
public decimal LastAsk
{
get { return lastAsk; }
set
{
lastAsk = value;
OnPropertyChanged(LastAskChangedArgs);
}
}

private int lastSize;
public int LastSize
{
get { return lastSize; }
set
{
lastSize = value;
OnPropertyChanged(LastSizeChangedArgs);
}
}

private int lastAskSize;
public int LastAskSize
{
get { return lastAskSize; }
set
{
lastAskSize = value;
OnPropertyChanged(LastAskSizeChangedArgs);
}
}

private int lastBidSize;
public int LastBidSize
{
get { return lastBidSize; }
set
{
lastBidSize = value;
OnPropertyChanged(LastBisSizeChangedArgs);
}
}

private static readonly PropertyChangedEventArgs LastPriceChangedArgs = ObservableHelper.CreateArgs(x => x.LastPrice);
private static readonly PropertyChangedEventArgs LastBidChangedArgs = ObservableHelper.CreateArgs(x => x.LastBid);
private static readonly PropertyChangedEventArgs LastAskChangedArgs = ObservableHelper.CreateArgs(x => x.LastAsk);
private static readonly PropertyChangedEventArgs LastSizeChangedArgs = ObservableHelper.CreateArgs(x => x.LastSize);
private static readonly PropertyChangedEventArgs LastBisSizeChangedArgs = ObservableHelper.CreateArgs(x => x.LastBidSize);
private static readonly PropertyChangedEventArgs LastAskSizeChangedArgs = ObservableHelper.CreateArgs(x => x.LastAskSize);

private void OnPropertyChanged(PropertyChangedEventArgs e)
{
var eventHandler = PropertyChanged;
if (eventHandler != null)
{
if (guiSyncContext != SynchronizationContext.Current)
{
guiSyncContext.CallOnMainThread(OnPropertyChanged, e);
}
else
{
eventHandler(this, e);
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private SynchronizationContext guiSyncContext = SynchronizationContext.Current;

public void SetGuiSyncContext()
{
guiSyncContext = SynchronizationContext.Current;
}
}
}