I have a main window with a viewmodel containing the properties:
class MainWindowModel : INotifyPropertyChanged { ObservableCollection<Currency> currencyCollection; string lastUpdated; int updateInterval; public CurrencyDataSource DataSource { get; set; } public ObservableCollection<Currency> CurrencyCollection { get { if (currencyCollection == null) currencyCollection = new ObservableCollection<Currency>(); return currencyCollection; } set { currencyCollection = value; OnPropertyChanged(); } } public string LastUpdated { get { return lastUpdated; } set { lastUpdated = value; OnPropertyChanged(); } } public int UpdateInterval { get { return updateInterval; } set { updateInterval = value; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName="") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); if (propertyName == "LastUpdated") { System.Diagnostics.Debug.WriteLine("Property changed from main " + propertyName + " " + this.LastUpdated); } } } Now, all my databindings with this VM works perfect in WPF interface databindings. I also have secondary windows who are opened from main, which have the following VM:
public class CurrencyTrackModel : INotifyPropertyChanged { Currency currency; string lastUpdated; public Currency Currency { get { return currency; } set { currency = value; } } public string LastUpdated { get { return lastUpdated; } set { lastUpdated = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); System.Diagnostics.Debug.WriteLine("Property changed from track " + propertyName + " " + this.LastUpdated); } } I initialize secondary windows from first as follows (I assign datacontext in constructor):
Currency currency = (sender as Button).DataContext as Currency; CurrencyTrackModel trackModel = new CurrencyTrackModel(); trackModel.Currency = currency; trackModel.LastUpdated = model.LastUpdated; currencyTrack trackWindow = new currencyTrack(trackModel); trackWindow.Show(); When the properties of Currency class are updated (within main window code-behind), UI bindings in both main and secondary windows are updated perfectly. However, the property LastUpdated is only updated in the UI of main window. On further investigation, I found out that trackModel.LastUpdated does not follow mainWindow.LastUpdated; it is fixed on the first value assigned (unlike properties of Currency).
I tried to use DateTime instead of string, but it did not work as well. I have little clue how to search it on internet; reference and other searches usually result in function passing pages.
Currency class (only fields, props are standard):
private decimal sellValue; private decimal buyValue; private decimal prevSellValue; private decimal prevBuyValue; private string sellValueColor; private string buyValueColor; private string sellArrow; private string buyArrow; Main window binding:
<TextBlock Text="{Binding Path=LastUpdated,StringFormat=last Updated: {0:F}}"></TextBlock> Secondary window binding:
<TextBlock Text="{Binding Path=LastUpdated, StringFormat=f}"></TextBlock>
trackModel.LastUpdated = model.LastUpdated;, trackmodel and model have somehow different objects, whereas it should be they are pointing to the same object. Moreover, WPF interfaces are usually specialized for databinding so they do not have issues in repainting.