WPF MVVM在视图模型属性,而不二传手? [英] WPF mvvm property in viewmodel without setter?
问题描述
我处理利用,坚持MVVM模式的一些问题WPF
I'm dealing with some WPF problems using and sticking to the MVVM pattern.
我的大部分特性是这样的:
Most of my properties look like this:
public string Period
{
get { return _primaryModel.Period; }
set
{
if (_primaryModel.Period != value)
{
_primaryModel.Period = value;
RaisePropertyChanged("Period");
}
}
}
这做工精良。
不过,我也有一些像这样的属性:
However I also have some properties like this:
public bool EnableConsignor
{
get
{
return (ConsignorViewModel.Id != 0);
}
}
它没有一个设定器作为id是改为自动(每次保存 ConsignorViewModel
之称。然而,这导致了问题的系统不知道什么时候从虚假到真实的布尔变化(因为没有 RaisePropertyChanged
被调用)。
It doesn't have a setter as the id is changed "automatically" (every time the save of ConsignorViewModel
is called. However this leads to the problem that the "system" doesn't know when the bool changes from false to true (as no RaisePropertyChanged
is called).
推荐答案
有关这些类型的属性,你只是需要筹集的PropertyChanged
时,相关的数据发生变化是这样的:
For these kinds of properties, you need to just raise PropertyChanged
when the dependent data is changed. Something like:
public object ConsignorViewModel
{
get { return consignorViewModel; }
set
{
consignorViewModel = value;
RaisePropertyChanged("ConsignorViewModel");
RaisePropertyChanged("EnableConsignor");
}
}
RaisePropertyChanged
可在任何方法调用的,所以只要把它的任何操作,将改变 EnableConsignor
进行。以上只是一个例子。
RaisePropertyChanged
can be invoked in any method, so just put it after whatever operation that would change the return value of EnableConsignor
is performed. The above was just an example.
这篇关于WPF MVVM在视图模型属性,而不二传手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!