【发布时间】:2011-09-30 15:30:54
【问题描述】:
我有一个 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 保护方法?
【问题讨论】:
-
关于显式附加的注释,带有内联 ItemTemplate 是的,您确实需要显式,如果您在单独的“视图”控件中定义模板,则绑定引擎将能够确定模板和因此你得到应用的标准约定......
标签: silverlight mvvm caliburn.micro