我正在使用MvvmCross和UICollectionView。 绑定工作完美,我正确显示了所有数据,即使我在CollectionView中选择了一个项目,它也可以在我的ViewModel中正确设置。 对于SelectedItem,我使用以下绑定:
set.Bind(_collectionViewSource).For(x => x.SelectedItem).To(vm => vm.SelectedMachine);
我唯一的问题是我想要最初选择第一个CollectionViewItem。
正如MvvmCross的消息来源所说当前不支持(在SelectedItem
的设置者中):
// note that we only expect this to be called from the control/Table
// we don't have any multi-select or any scroll into view functionality here
那么,执行项目初始预选的最佳方法是什么?我可以拨打_collectionView.SelectItem
的地方是什么地方?
我尝试在收集更改时调用它,但这似乎不起作用。
答案 0 :(得分:3)
如果您需要此功能,您应该能够从MvxCollectionViewSource继承并添加类似
的属性 public event EventHandler SelectedItemExChanged;
public object SelectedItemEx
{
get { return base.SelectedItem; }
set
{
base.SelectedItem = value;
var index = FindIndexPath(value); // find the NSIndexPath of value in the collection
if (index != null)
_collectionView.SelectItem(index, true, UICollectionViewScrollPosition.CenteredHorizontally);
var handler = SelectedItemExChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
然后可以绑定而不是SelectedItem
我可以调用_collectionView.SelectItem的地方是什么?我尝试在收集更改时调用它,但这似乎不起作用。
如果这不起作用,那么我不确定 - 您可能正在进入动画计时问题 - 请参阅uicollectionview select an item immediately after reloaddata?之类的问题 - 或者尝试编辑您的问题以发布更多信息你的代码 - 人们可以通过调试更容易希望的东西。