I want to pass SecondViewModel SecondProperty value to ViewModel myProperty and show the value on TextBlock. i want the coding to be done in SecondViewModel. Hope it is clear.
Thanks for the help in Advance.
View:
<TextBlock Text="{Binding Path=myProperty}"/>
ViewModel:
private int _myProperty;
public int myProperty
{
get { return _myProperty; }
set { _myProperty = value; OnPropertyChanged("myProperty"); }
}
SecondViewModel:
private int _secondProperty;
public int SecondProperty
{
get { return _secondProperty; }
set { _secondProperty = value; OnPropertyChanged("SecondProperty"); }
}
From your comment, assuming that
ViewModel
holds a collection ofSecondViewModel
items, you need to set thePropertyChangedEvent
for each instance ofSecondViewModel
to triggerViewModel.myProperty
to refresh. e.g. ...As an aside, you should never call
OnPropertyChanged()
with a "magic string" - usenameof()
instead.If you need that, you should create a VM DataSource to create a dependency in your first VM.
When you create your VMs then use this class to create the dependency. Every time
SecondViewModel
SecondProperty changes, theViewModel
myProperty will be raised. I hope it's clear, let me know if you need something else