-
20-12-2019 - |
题
我有一个按照MVVM方案的应用程序。我有多个视图和视图。 在我的主页上,我有一个TextBlock我想用来自所选元素的信息更新。
启动应用程序时,我从MainViewModel插入值以测试绑定,因此在此处工作。代码如下:
<TextBlock Text="{Binding colorOfElement}" Grid.Row="1"/>
.
mainviewmodel中的代码
private string _colorOfElement;
public string colorOfElement
{
get
{
return _colorOfElement;
}
set
{
_colorOfElement = value;
NotifyPropertyChanged("colorOfElement");
}
}
.
......
colorOfElement = "Test";
.
这是正确显示的。当用户然后用元素交互时,在新视图中触发事件时,在这里,我对MainViewModel的引用是引用,因此我可以轻松更新字符串colorofelement。
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;
}
.
当此函数执行时,我发送到MainViewModel并解雇NotifyPropertyChanged。但是在应用程序中.Page显示了我无法看到变量的任何更新,而是在变量更改的代码中。任何绑定问题的想法?
答案 正如我所测试的那样,Datacontext,一切都在工作的问题是我的公共班级。
public class MainViewModel : ViewModelBase
.
此处应包括InotifyPropertyNged接口以启用该功能。所以简单的解决方案是添加这个并获得:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
.
然后你很好:)
解决方案
确保:
- datacontext设置为mainviewModel的右侧实例 mainviewModel实现了InotifyPropertynanged接口
不隶属于 StackOverflow