【发布时间】:2024-04-14 16:20:01
【问题描述】:
我的上下文:实体框架 4.0 + WCF Ria 数据服务 + Silverlight 4。 假设我有来自 EF 的实体 People,然后我创建了 PeopleVM,例如:
public class PeopleViewModel : ViewModelBase
{
public PeopleViewModel(){
//....
this._hasChanges = MyDomainContext.HasChanges;
}
private People _People;
public People People
{
get { return _People; }
set
{
if (value != this._People)
{
value = this._People;
this.RaisePropertyChanged("People");
}
}
}
private bool _hasChanges;
public bool HasChanges
{
get { return this._hasChanges; }
set
{
if (this._hasChanges != value)
{
this._hasChanges = value;
this.RaisePropertyChanged("HasChanges");
}
}
}
//........
private RelayCommand _saveCommand = null;
public RelayCommand SaveCommand
{
//.....
}
private RelayCommand _cancelCommand = null;
public RelayCommand CancelCommand
{
//.....
}
}
在 UI xaml 中,我将绑定设置为:
<TextBox Text="{Binding People.FirstName, Mode=TwoWay}" />
<TextBox Text="{Binding People.LasttName, Mode=TwoWay}" />
然后我将 UI 中的按钮设置为:
<Button Content="Save" Command="{Binding SaveCommand}" IsEnabled="{Binding HasChanges}"/>
<Button Content="Undo" Command="{Binding CancelCommand}" IsEnabled="{Binding HasChanges}"/>
所以我想要的是:
最初,应该禁用按钮,因为没有数据更改。
当用户在 FirstName、LastName 文本框中输入一些内容时,该按钮应启用。
但是使用上面的代码,即使我改变了名字,姓氏,按钮仍然处于禁用状态。
如何解决这个问题?
【问题讨论】:
-
您确定在实例化
PeopleViewModel时MyDomainContext.HasChanges的值不为空吗?尝试在那里放置断点。
标签: mvvm silverlight-4.0 entity-framework-4 wcf-ria-services