C# 视图元素的MVVM更新

C# 视图元素的MVVM更新,c#,mvvm,binding,windows-phone-8,C#,Mvvm,Binding,Windows Phone 8,我有一个遵循MVVM方案的应用程序。其中我有多个视图和视图模型。 在我的主页上,我有一个文本块,我想用所选元素的信息更新它 在启动应用程序时,我从mainviewmodel插入一个值来测试绑定,这样一切都可以在这里工作。其中代码如下: <TextBlock Text="{Binding colorOfElement}" Grid.Row="1"/> colorOfElement = "Test"; 这是正确显示的。当用户与一个元素交互时,一个事件在新的viewmodel中被触发

我有一个遵循MVVM方案的应用程序。其中我有多个视图和视图模型。 在我的主页上,我有一个文本块,我想用所选元素的信息更新它

在启动应用程序时,我从mainviewmodel插入一个值来测试绑定,这样一切都可以在这里工作。其中代码如下:

<TextBlock Text="{Binding colorOfElement}" Grid.Row="1"/>

colorOfElement = "Test";
这是正确显示的。当用户与一个元素交互时,一个事件在新的viewmodel中被触发,在这里我有一个对mainviewmodel的引用,因此我可以轻松地更新字符串colorofeelement

private MainViewModel mv;
......
    public void MouseDown(ManipulationStartedEventArgs obj)
    {
        FrameworkElement MovingElement = (FrameworkElement)obj.OriginalSource;

        Canvas canvas = FindParentOfType<Canvas>(MovingGear);

        obj.ManipulationContainer = canvas;
        obj.Handled = true;

        testViewModel viewModel = (testViewModel)MovingElement.DataContext;

        mv.colorOfElement = viewModel.model.Color;
    }
在这里,它应该包括INotifyPropertyChanged接口以启用该功能。因此,简单的解决方案是添加此项并获得:

     public class MainViewModel : ViewModelBase, INotifyPropertyChanged
然后你就可以出发了:)

确保:

  • DataContext设置为MainViewModel的正确实例
  • MainViewModel实现INotifyPropertyChanged接口

视图似乎未接收NotifyPropertyChanged。是否确实将MainViewModel设置为要更新的视图的DataContext?我已在视图中设置DataContext,这使我能够在启动时设置变量。我同意没有收到,但我不知道为什么。谢谢你的帮助,忘记了界面没有被采用。
     public class MainViewModel : ViewModelBase
     public class MainViewModel : ViewModelBase, INotifyPropertyChanged