MainPage.xaml
<TextBlock Text="{Binding Pathname, Source={StaticResource ViewModel}, Mode=OneWay}" />
App.xaml
<ResourceDictionary>
<vm:InspectViewModel x:Key="ViewModel" />
</ResourceDictionary>
ViewModel
private string _pathname = null;
public string Pathname
{
get { return _pathname; }
set
{
if (_pathname != value)
{
_pathname = value;
RaisePropertyChanged("Pathname");
}
}
}
public void UpdatePathname(string path)
{
Pathname = path;
}
MainPage CodeBehind
private void lazyNavTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
InspectViewModel vm = new InspectViewModel();
var path = view.GetPath().ToArray();
string pathname = null;
// to figure out what the pathname is
for (int i = 0; i < path.Count(); i++)
{
TreeList treeItem = (TreeList)path[i].Key;
if (i == path.Count()-1)
pathname = pathname + treeItem.Name;
else
pathname = pathname + treeItem.Name + " : ";
}
vm.UpdatePathname(pathname);
}
The bound TextBlock shows nothing, nada, zip. The pathname shource is changing correctly but nothing seems to happen when I fire the INotifyPropertyChanged event on change.
I am sure I’m missing something really obvious but I can’t figure out what!
You are creating 2 instances of your ViewModel:
InspectViewModel vm = new InspectViewModel(), this is the modified instance)You should use single instance of you ViewModel, for example,
instead of creating it in MainPage code-behind.