强制重新评估 Caliburn Micro 中的防护方法

发布于 2024-11-19 02:43:11 字数 1510 浏览 3 评论 0原文

我有一个 ItemsControl,每个项目都有向上移动、向下移动和删除按钮(通过模板)。我的 ItemsControl 源绑定到项目集合,这些项目是模型 DataContracts/POCO(而不是 VM)。

我已在我的主页的视图模型中附加了一个 Caliburn 消息处理程序。

<Button cal:Message.Attach="MoveUp($dataContext)" >Up</Button>
<Button cal:Message.Attach="MoveDown($dataContext)" >Down</Button>

我相信我必须明确使用 cal:Message.Attach 并且不依赖约定,因为我在我的 ItemTemplate 中。

查看模型:

    ObservableCollection<Item> MyCollection = new ObservableCollection<Item>();  
    //Item class is simple -- only has a string Name property 

    public bool CanMoveUp(Item item)
    {
        var index = MyCollection.IndexOf(item);
        return index > 0;
    }

    public void MoveUp(Item item)
    {
        var index = MyCollection.IndexOf(item);
        if (index > 0)
        {
            MyCollection.Remove(item);
            MyCollection.Insert(index - 1, item);
        }
    }           


    public bool CanMoveDown(Item item)
    {
        var index = MyCollection.IndexOf(item);
        return index > -1 && index < class1.Count - 1;
    }

    public void MoveDown(Item item)
    {
        var index = MyCollection.IndexOf(item);
        if (index > -1 && index < class1.Count - 1)
        {
            MyCollection.Remove(item);
            MyCollection.Insert(index + 1, item);
        }
    }

第一个项目的向上按钮最初被禁用。当我向下移动第一个项目时,第二个项目将成为第一个项目,但其向上按钮不会自动禁用。如何强制重新评估 CanMoveUp 保护方法?

I have an ItemsControl which has a move up, move down, and delete button in each item (via template). My ItemsControl source binds to a collection of Items which are model DataContracts/POCOs (not VMs).

I've attached a Caliburn message handler in my main page's view model.

<Button cal:Message.Attach="MoveUp($dataContext)" >Up</Button>
<Button cal:Message.Attach="MoveDown($dataContext)" >Down</Button>

I believe I had to be explicit with cal:Message.Attach and not rely on convention because I was within my ItemTemplate.

View Model:

    ObservableCollection<Item> MyCollection = new ObservableCollection<Item>();  
    //Item class is simple -- only has a string Name property 

    public bool CanMoveUp(Item item)
    {
        var index = MyCollection.IndexOf(item);
        return index > 0;
    }

    public void MoveUp(Item item)
    {
        var index = MyCollection.IndexOf(item);
        if (index > 0)
        {
            MyCollection.Remove(item);
            MyCollection.Insert(index - 1, item);
        }
    }           


    public bool CanMoveDown(Item item)
    {
        var index = MyCollection.IndexOf(item);
        return index > -1 && index < class1.Count - 1;
    }

    public void MoveDown(Item item)
    {
        var index = MyCollection.IndexOf(item);
        if (index > -1 && index < class1.Count - 1)
        {
            MyCollection.Remove(item);
            MyCollection.Insert(index + 1, item);
        }
    }

The first item's up button is initially disabled. When I move down the first item, the 2nd item becomes the first item, but its up button is not automatically disabled. How do I force reevaluation of the CanMoveUp guard method?

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

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

发布评论

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

评论(2

雨后彩虹 2024-11-26 02:43:11

MoveUpMoveDown 方法中,您可以使用 NotifyOfPropertyChanged(( ) => this.CanMoveUp); (或 this.CanMoveDown,视情况而定)。

这是假设您的 ViewModel 派生自 ScreenConductorPropertyChangedBase,它应该是这样的。

更新

您需要做的是重构代码,将 ListBoxSelectedItem 绑定到类型为 < 的视图模型上的属性。代码>项目。事实上,Caliburn.Micro 会为您执行此操作,例如,如果您的 ListBox 有一个 x:Name="Items",它会查找名为 SelectedItem (它还搜索其他约定)。然后,您的 CanMoveUp/Down 方法可以是属性,而不是检查 SelectedItem 属性,因此您可以使用 NotifyOfPropertyChanged() 使这些绑定无效。

In your MoveUp and MoveDown methods, you can notify the UI that the bindings for CanMoveUp/Down have been invalidated using NotifyOfPropertyChanged(() => this.CanMoveUp); (or this.CanMoveDown as appropriate).

This is assuming your ViewModel derives from Screen, Conductor, or PropertyChangedBase which it should.

Update

What you'll want to do is refactor the code to bind the SelectedItem of the ListBox to a property on your view model of type Item. In fact, Caliburn.Micro will do this for you, if your ListBox has an x:Name="Items" for example, it will look for a property called SelectedItem (there are other conventions too that it searches for). Then, your CanMoveUp/Down methods can be properties instead that check the SelectedItem property, and therefore you can use NotifyOfPropertyChanged() to invalidate those bindings.

阪姬 2024-11-26 02:43:11

我最终将我的 Item 类提升为成熟的 ViewModel。我使用了 IoC / MEF,以便 Item 类可以找到它的容器,这样它就可以判断它是容器中的第一个还是最后一个项目。如果我需要强制重新评估,我创建了一个重新评估防护属性的方法:

public void ReevaluateButtons()
{
    NotifyOfPropertyChange(() => CanMoveDown);
    NotifyOfPropertyChange(() => CanMoveUp);
}

仍然不知道是否有一种方法可以强制重新评估防护方法

NotifyOfPropertyChange(() => CanMoveDown(item));

I ended up promoting my Item class to a full fledged ViewModel. I used IoC / MEF so that the Item class could find its container, so it could tell if it was the first or last item in the container. If I need to force reevaluation, I created a method that reevaluates guard properties:

public void ReevaluateButtons()
{
    NotifyOfPropertyChange(() => CanMoveDown);
    NotifyOfPropertyChange(() => CanMoveUp);
}

Still don't know if there is yet a way to force reevaluation of guard methods

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