我最近开始试图弄到似乎还没有工作的MVVM。MVVM模式,ViewModel集合
我有我的模型,视图和ViewModels。我有一个使用INotifyPropertyChanged
接口的baseview模型。
我想收集我的ViewModels
,这样我就可以在我的所有视图中使用我的数据。但我似乎无法完成这一切。
无论如何,在阅读大量不同的东西后,我甚至不知道应该是什么。
我希望有人能回答我的最大问题是我在哪里转储我的ViewModels
收藏?我需要在View
中更改一个ViewModel
,并在另一个中再次显示它。
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public BaseViewModel()
{
}
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是一个的填充在一个视图中ViewModel
,则需要在另一个视图显示。但我不知道如何完成ObservableCollection<T>
课程。
public class WorkViewModel : BaseViewModel
{
private string reference;
private string address;
private string scope;
private string cost;
private double amount;
private double gst;
private double total;
public string Reference
{
get { return reference; }
set
{
if (reference != value)
{ reference = value; RaisePropertyChanged("Reference"); }
}
}
public string Address
{
get { return address; }
set
{
if (address != value)
{ address = value; RaisePropertyChanged("Address"); }
}
}
public string Scope
{
get { return scope; }
set
{
if (scope != value)
{ scope = value; RaisePropertyChanged("Scope"); }
}
}
public string Cost
{
get { return cost; }
set
{
if (cost != value)
{ cost = value; RaisePropertyChanged("Cost"); }
}
}
public double Amount
{
get { return amount; }
set
{
if (amount != value)
{
amount = value;
GST = Math.Round(amount * 0.10,2);
RaisePropertyChanged("Amount");
}
}
}
public double GST
{
get { return gst; }
set
{
if (gst != value)
{
gst = value;
Total = Math.Round(Amount + GST,2);
RaisePropertyChanged("GST");
}
}
}
public double Total
{
get { return total; }
set
{
if (total != value)
{
total = value;
RaisePropertyChanged("Total");
}
}
}
}
我已经试过这样:就像我需要做一个单身
Tab1ViewModel vm = (ViewModels.Where(vm => vm is Tab1ViewModel).Count() == 0) ? new Tab1ViewModel(): (ViewModels.Where(vm => vm is Tab1ViewModel).FirstOrDefault() as Tab1ViewModel;"
看来:
"Create a BaseViewModel and a Collection ObservableCollection<BaseViewModel> _viewModels;
Create a Property ObservableCollection<BaseViewModel> ViewModels around _viewModels
Define your View Models like this and add to Collection
MainViewModel : BaseViewModel
Tab1ViewModel : BaseViewModel
Tab2ViewModel : BaseViewModel
现在你可以使用这个?
请添加一个例子,有问题的代码... – Blachshma
2个字:卡利微 –