Caliburn Can{Action} 未在 Refresh() 上更新

发布于 2024-10-21 05:24:58 字数 1619 浏览 0 评论 0原文

    public void AddProfile()
    {
        //Add conventions for DX Components.
        Profile newProfile = new Profile()
        {
            Description = "New Profile",
            DisplayOrder = decimal.MaxValue,
            IsActive = true,
            IsDefault = false,
            IsSelected = true,
            ProfileId = 0
        };
        EditProfileViewModel profile = new EditProfileViewModel(true) { Profile = newProfile };


        if (windowManager.ShowDialog(profile,null ) ?? false) // ?? means (coallesce so if null use false value) the line means, if dialog returns true...
        {
            Profiles.Add(profile.Profile);
            NotifyOfPropertyChange(string.Empty);
        }

    }

可以添加按钮的代码是这样的。

    public bool CanAddAllToProfile
    {
        get
        {
            var p = Profiles.Where(x => x.IsSelected).FirstOrDefault();
            if (p == null)
                return false;
            if (AvailableModules.Count() == 0)
                return false;
            return true;
        }
    }

    public void AddAllToProfile()
    {
        var p = Profiles.Where(x => x.IsSelected).FirstOrDefault();
        if (p == null)
            return;
        foreach (var m in AvailableModules)
            p.Modules.Add(m);
        NotifyOfPropertyChange(string.Empty);
    }

如果我编写这样的代码,则 CanAddAllToProfile get 不会被执行。

如果我执行 NotifyOfPropertyChange(() => CanAddAllToProfile) 它有效

我也尝试过 Refresh();

我从 Screen 继承视图模型,任何想法我都有一堆其他需要执行的 CanExecuteBindings。显然这是可以解决的,但我想知道我是否做错了什么。

    public void AddProfile()
    {
        //Add conventions for DX Components.
        Profile newProfile = new Profile()
        {
            Description = "New Profile",
            DisplayOrder = decimal.MaxValue,
            IsActive = true,
            IsDefault = false,
            IsSelected = true,
            ProfileId = 0
        };
        EditProfileViewModel profile = new EditProfileViewModel(true) { Profile = newProfile };


        if (windowManager.ShowDialog(profile,null ) ?? false) // ?? means (coallesce so if null use false value) the line means, if dialog returns true...
        {
            Profiles.Add(profile.Profile);
            NotifyOfPropertyChange(string.Empty);
        }

    }

The code for the can add buttons is like this.

    public bool CanAddAllToProfile
    {
        get
        {
            var p = Profiles.Where(x => x.IsSelected).FirstOrDefault();
            if (p == null)
                return false;
            if (AvailableModules.Count() == 0)
                return false;
            return true;
        }
    }

    public void AddAllToProfile()
    {
        var p = Profiles.Where(x => x.IsSelected).FirstOrDefault();
        if (p == null)
            return;
        foreach (var m in AvailableModules)
            p.Modules.Add(m);
        NotifyOfPropertyChange(string.Empty);
    }

The CanAddAllToProfile get does not get executed if I write the code like this.

if I do a NotifyOfPropertyChange(() => CanAddAllToProfile) it works

I also tried Refresh();

I am inheriting the viewmodel from Screen any ideas I have a bunch of other CanExecuteBindings that need to be executed. Obviously this can be worked around but I am wondering if I am doing something wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

纵性 2024-10-28 05:24:58

您刚刚给出了问题的答案:

NotifyOfPropertyChange(() => CanAddAllToProfile);

这是告诉绑定基础结构它应该调用 CanAddAllToProfile 并更新绑定到该属性的任何内容的适当方法(例如名为 AddAllToProfile 的按钮) )。那么,如果它有效,你为什么不这样做呢?

You just gave the answer in your question:

NotifyOfPropertyChange(() => CanAddAllToProfile);

That is the appropriate way to tell the binding infrastructure that it should call CanAddAllToProfile and update anything that is binding to that property (such as the button named AddAllToProfile). So if it works, why are you not doing that?

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