强制重新评估 Caliburn Micro 中的防护方法
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
MoveUp
和MoveDown
方法中,您可以使用NotifyOfPropertyChanged(( ) => this.CanMoveUp);
(或 this.CanMoveDown,视情况而定)。这是假设您的 ViewModel 派生自
Screen
、Conductor
或PropertyChangedBase
,它应该是这样的。更新
您需要做的是重构代码,将
ListBox
的SelectedItem
绑定到类型为 < 的视图模型上的属性。代码>项目。事实上,Caliburn.Micro 会为您执行此操作,例如,如果您的ListBox
有一个x:Name="Items"
,它会查找名为SelectedItem
(它还搜索其他约定)。然后,您的CanMoveUp/Down
方法可以是属性,而不是检查SelectedItem
属性,因此您可以使用NotifyOfPropertyChanged()
使这些绑定无效。In your
MoveUp
andMoveDown
methods, you can notify the UI that the bindings forCanMoveUp/Down
have been invalidated usingNotifyOfPropertyChanged(() => this.CanMoveUp);
(or this.CanMoveDown as appropriate).This is assuming your ViewModel derives from
Screen
,Conductor
, orPropertyChangedBase
which it should.Update
What you'll want to do is refactor the code to bind the
SelectedItem
of theListBox
to a property on your view model of typeItem
. In fact, Caliburn.Micro will do this for you, if yourListBox
has anx:Name="Items"
for example, it will look for a property calledSelectedItem
(there are other conventions too that it searches for). Then, yourCanMoveUp/Down
methods can be properties instead that check theSelectedItem
property, and therefore you can useNotifyOfPropertyChanged()
to invalidate those bindings.我最终将我的 Item 类提升为成熟的 ViewModel。我使用了 IoC / MEF,以便 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:
Still don't know if there is yet a way to force reevaluation of guard methods