我想学习MVVM。我理解这个概念,然而,我对这个绑定感到困惑。我不确定在哪里绑定我的Fill属性。请帮忙。 Tqvm先进。Wpf绑定混淆
视图 - 名称:MainScreen.xaml
<Path Fill="{Binding mainScreenClass, Converter={StaticResource colorConverter}}"/>
inCodeBehind
DataContext = new vmMainScreen();
视图模型 - 名称:vmMainScreen
public ICommand cmdMouseEnterNav { get; private set; }
public mMainScreen mainScreenClass { get; set; }
public vmMainScreen()
{
mainScreenClass = new mMainScreen();
mainScreenClass.propNaviconFill = new SolidColorBrush(Colors.White);
naviconMouseEventChecker();
}
private void naviconMouseEventChecker()
{
cmdMouseEnterNav = new SimpleCommand
{
ExecuteDelegate = x => mainScreenClass.propNaviconFill = (SolidColorBrush)(new BrushConverter().ConvertFrom("#c5a02b"))
};
}
型号 - 名称:mMainScreen
public class mMainScreen : INotifyPropertyChanged
{
private Brush _NaviconFill = new SolidColorBrush(Colors.White);
public Brush propNaviconFill
{
get
{
return this._NaviconFill;
}
set
{
this._NaviconFill = value;
NotifyPropertyChanged("propNaviconFill");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
我明白,当我在我的colorConverter断点时,我正在上课。不是PropNaviconFill的财产。如果我使用我的ViewModel上的Brush类创建另一个属性并将其绑定到Fill,则没有问题。但这意味着我没有遵循MVVM的正确结构。再次感谢。
oo所以datacontext的观点应该去模型?那是MVVM的正确结构吗?查看 - >型号 - >视图模型?对于INotifyPropertyChanged,我想我已经设法通知ViewModel中现有属性的更改视图。我使用Brush类创建了一个属性,我的视图绑定到属性,并且它已经可以工作。 –
@ArifSam MVVM(模型视图视图模型)是一种模式,其中视图模型是视图和模型之间的粘合代码,因此视图不必直接与模型交互,反之亦然。只有视图模型应该实现'INotifyPropertyChanged'。模型应该只是POCO(简单的旧CLR对象)类,只需要很少或没有操作。让您的视图模型为您的模型执行操作。 –
我真的有点太糊涂了。哈哈。对不起,如果我继续问同样的问题。根据你的回答,我需要将dataContext附加到Model中?基于此,Model属性的任何更改都应该通知View。然而,主要的操作,这意味着我的命令是在ViewModel和我的ViewModel构造函数没有被调用,因为我们已经改变了dataContext = new Model();.我们在哪里实例化ViewModel?再次感谢你。 –