2014-06-09 28 views
2

我正在使用MvvmCross和UICollectionView。 綁定工作完美,我的所有數據都正確顯示,即使我在CollectionView中選擇一個項目,它也會在我的ViewModel中正確設置。 對於我的SelectedItem使用以下綁定:MvvmCross和UICollectionView如何將SelectedItem從VM綁定到視圖

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來自哪裏?

我試着在收集改變時調用它,但那似乎不起作用。

回答

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?問題 - 也許嘗試編輯您的問題張貼多一點你的代碼 - 這人們可以更容易地希望與調試。

+0

謝謝Stuart,我會將其標記爲答案,因爲它是完全正確的。對我來說,我只是愚蠢的使用'NSIndexPath.FromIndex'而不是'NSIndexPath.FromItemSection'。現在使用後者,一切都很好。 – Shaddix

相關問題