WPF Caliburn:识别集合项的值何时发生变化
我有一个 XamDataPresenter (XamDataGrid) 绑定到 ViewModel 中的集合:
XAML:
<igDP:XamDataPresenter x:Name="dataPresenter" DataSource="{Binding Path=AppServers, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">
</igDP:XamDataPresenter>
代码:
public ShellViewModel()
{
AppServers = new BindingListCollectionView(new BindingList<AppServer>(_context.GetAllAppServers()));
AppServers.CurrentChanged += new EventHandler(AppServers_CurrentChanged);
}
void AppServers_CurrentChanged(object sender, EventArgs e)
{
NotifyOfPropertyChange(() => CanSaveAppServers);
NotifyOfPropertyChange(() => CanDeleteAppServers);
}
CanSaveAppServers 属性:
public bool CanSaveAppServers
{
get
{
return (_appServers.SourceCollection as BindingList<AppServer>).Any(x => x.ChangeTracker.State != ObjectState.Unchanged);
}
}
如果更改集合中的某个项目,则 CanSaveAppServers 属性应该为 false。但是 CanSaveAppServers 是如何调用的呢?另一个事件?或者收集类型错误?难道这不应该以某种方式自动完成吗?
提前致谢。
i have an XamDataPresenter (XamDataGrid) bound to a collection in the ViewModel:
XAML:
<igDP:XamDataPresenter x:Name="dataPresenter" DataSource="{Binding Path=AppServers, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">
</igDP:XamDataPresenter>
Code:
public ShellViewModel()
{
AppServers = new BindingListCollectionView(new BindingList<AppServer>(_context.GetAllAppServers()));
AppServers.CurrentChanged += new EventHandler(AppServers_CurrentChanged);
}
void AppServers_CurrentChanged(object sender, EventArgs e)
{
NotifyOfPropertyChange(() => CanSaveAppServers);
NotifyOfPropertyChange(() => CanDeleteAppServers);
}
The CanSaveAppServers property:
public bool CanSaveAppServers
{
get
{
return (_appServers.SourceCollection as BindingList<AppServer>).Any(x => x.ChangeTracker.State != ObjectState.Unchanged);
}
}
The CanSaveAppServers property should be false if an item of the collection is changed. But how is the CanSaveAppServers called? Another event? Or the wrong collection type? Shouldn't this be done automatically in some way?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您让 Caliburn 通过命名约定进行绑定,那么您就有一个名为
SaveAppServers
的公共方法。 Caliburn 创建一个绑定到Button
的ICommand
,这样当单击按钮时,ICommand
的Execute() 被调用。同时,
ICommand
上有一个CanExecute()
方法,用于确定按钮是否启用。当您调用
NotifyOfPropertyChange(() => CanSaveAppServers)
时,最终会使ICommand
引发其CanExecuteChanged
事件,从而使 WPF 刷新再次调用CanExecute()
,在幕后获取CanSaveAppServers
。If you are letting Caliburn bind via naming conventions, then you have a public method named
SaveAppServers
. Caliburn creates anICommand
that is bound to theButton
so that when the button is clicked,ICommand
'sExecute()
is called. In the meantime, there is aCanExecute()
method onICommand
that is used to determine whether the button is enabled or not.When you call
NotifyOfPropertyChange(() => CanSaveAppServers)
, this ends up making theICommand
raise itsCanExecuteChanged
event, which makes WPF refresh by callingCanExecute()
again, which under the covers is gettingCanSaveAppServers
.