Reputation:
I need to change a value of property TextSource
from another ViewModel, so I did this to get ReaderViewModel.
ViewerViewModel.cs:
private ReaderViewModel _readerViewModel = new ReaderViewModel();
public ReaderViewModel ReaderViewModel
{
get => _readerViewModel;
set
{
_readerViewModel = value;
OnPropertyChanged("");
}
}
As well as changing the property's value to something:
ReaderViewModel.TextSource = "some string";
ReaderViewModel.cs:
private string _textSource;
public string TextSource
{
get => _textSource;
set
{
_textSource = value;
OnPropertyChanged();
}
}
ReaderPage.xaml:
<Page.DataContext>
<viewModels:ReaderViewModel/>
</Page.DataContext>
...
<Grid>
<TextBox Text="{Binding TextSource}"/>
<Grid/>
But it don't work at all, TextBox is still empty. I can't really figure out, where's the problem. The DataContext for ReaderPage is already set to the right ViewModel.
Upvotes: 1
Views: 449
Reputation: 169150
I can't really figure out, where's the problem
You are creating one instance of the ReaderViewModel
in the view:
<Page.DataContext>
<viewModels:ReaderViewModel/>
</Page.DataContext>
...and another one in the ViewerViewModel
:
private ReaderViewModel _readerViewModel = new ReaderViewModel();
Setting the TextSource
property of the latter instance won't affect the view that is bound to the former instance.
You should make sure that there is only one instance of the ReaderViewModel
involved.
Upvotes: 4