WPF Caliburn:识别集合项的值何时发生变化

发布于 2024-10-27 08:36:15 字数 1173 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2024-11-03 08:36:15

如果您让 Caliburn 通过命名约定进行绑定,那么您就有一个名为 SaveAppServers 的公共方法。 Caliburn 创建一个绑定到 ButtonICommand,这样当单击按钮时,ICommandExecute() 被调用。同时,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 an ICommand that is bound to the Button so that when the button is clicked, ICommand's Execute() is called. In the meantime, there is a CanExecute() method on ICommand that is used to determine whether the button is enabled or not.

When you call NotifyOfPropertyChange(() => CanSaveAppServers), this ends up making the ICommand raise its CanExecuteChanged event, which makes WPF refresh by calling CanExecute() again, which under the covers is getting CanSaveAppServers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文