开发者

WPF Caliburn: Recognize when values of collection items change

开发者 https://www.devze.com 2023-02-20 13:14 出处:网络
i have an XamDataPresenter (XamDataGrid) bound to a collection in the ViewModel: XAML: <igDP:XamDataPresenter x:Name=\"dataPresenter\" DataSource开发者_JAVA技巧=\"{Binding Path=AppServers, Update

i have an XamDataPresenter (XamDataGrid) bound to a collection in the ViewModel:

XAML:

<igDP:XamDataPresenter x:Name="dataPresenter" DataSource开发者_JAVA技巧="{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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消