2014-03-31 29 views
1

嗨我有一個問題,試圖綁定MvxTableViewCell附件複選標記到本地屬性。我試過以下的Bind MvxBindableTableViewCell's Accessory to booleanMvvMCross UITableView將本地屬性綁定到UITableViewCellAccessory.CheckMark

找到了一個例子,我很新的IOS,甚至更新的版本來MvvmCross所以我道歉,如果我做任何愚蠢的錯誤

public partial class TaxaListCellView : MvxTableViewCell 
{ 
    public static readonly UINib Nib = UINib.FromName ("TaxaListCellView", NSBundle.MainBundle); 
    public static readonly NSString Key = new NSString ("TaxaListCellView"); 


    public TaxaListCellView (IntPtr handle) : base (handle) 
    { 
     Accessory = UITableViewCellAccessory.Checkmark; 
     SelectionStyle = UITableViewCellSelectionStyle.None; 

     this.DelayBind (() => { 
      var set = this.CreateBindingSet<TaxaListCellView, TaxonViewModel>(); 
      set.Bind(lblSelectedTaxon).To(s => s.Name); 
//I've been playing around with both ways below, and a few different 
//variants without any success 
      //set.Bind("IsChecked").To(s => s.IsSelected).TwoWay(); 
      //set.Bind(@"'IsChecked':{'Path':'IsSelected'"); 
      set.Apply(); 
     }); 
    } 

    public bool IsChecked 
    { 
     get { return Accessory == UITableViewCellAccessory.Checkmark; } 
     set { Accessory = value ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; } 
    } 

    public static TaxaListCellView Create() 
    { 
     return (TaxaListCellView)Nib.Instantiate (null, null) [0]; 
    } 

    public override void SetSelected (bool selected, bool animated) 
    { 
     Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; 

     base.SetSelected (selected, animated); 
    } 
} 

我不知道,如果有什麼毛病我MvxTableViewController但這裏的代碼

public class TaxaListView : MvxTableViewController 
{ 
    public TaxaListView() 
    { 
     Title = "Taxon List"; 
    } 

    private UISearchBar _searchBar; 


    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     _searchBar = new UISearchBar(new RectangleF(0,0,320, 44)) 
     { 
      AutocorrectionType = UITextAutocorrectionType.Yes,     
     }; 
     _searchBar.SearchButtonClicked += SearchBar_SearchButtonClicked; 
     _searchBar.TextChanged += SearchBarOnTextChanged; 

     var source = new MvxSimpleTableViewSource(TableView, TaxaListCellView.Key, TaxaListCellView.Key); 

     var set = this.CreateBindingSet<TaxaListView, TaxaListViewModel>(); 
     set.Bind (source).To (vm => vm.Taxa); 
     set.Bind (source) 
     .For (s => s.SelectionChangedCommand) 
     .To (vm => vm.ItemSelectedCommand); 

     set.Apply(); 

     TableView.RowHeight = 50; 
     TableView.Source = source; 
     TableView.AllowsSelection = true; 
     TableView.AllowsSelectionDuringEditing = true; 
     TableView.TableHeaderView = _searchBar; 

     TableView.ReloadData(); 
    } 

    private void SearchBarOnTextChanged(object sender, UISearchBarTextChangedEventArgs uiSearchBarTextChangedEventArgs) 
    { 
     if(string.IsNullOrWhiteSpace(_searchBar.Text)) 
     { 
      ((TaxaListViewModel) ViewModel).SearchTaxaByText(string.Empty); 
     } 
    } 

    void SearchBar_SearchButtonClicked(object sender, System.EventArgs e) 
    { 
     ((TaxaListViewModel)ViewModel).SearchTaxaByText(_searchBar.Text); 
    } 
} 

當我從列表中選擇第一 When I Select an item from the list first 當我開始搜索的項目,或者是即使我去BAC k加入項目列表 When I start searching, Or happens even if I go back into the List of items

+0

你確定在你的UITableView中允許選擇嗎?當你選擇其中一行時,SetSelected觸發器會被執行嗎? – choper

+0

嗨@choper,對不起,這是有用的,這是如果當我回到列表中,我失去了支票標記,或者如果我使用搜索欄,我失去了選中的選中標記。我會發布兩個屏幕抓圖來更好地解釋它。請回復 – InitLipton

+0

嘗試告訴ViewModel該屬性已更改 - 添加一個「公共事件EventHandler IsChecked;」,然後在屬性更改時從單元中觸發此事件 - 例如,在'SetSelected'期間。這**可能會奏效(沒有它 - 沒有任何事情告訴Cell的ViewModel該項目已經改變) – Stuart

回答

2

正如Stuart也提到的那樣,我需要告訴ViewModel其值已經改變。 我刪除,因爲這將SetSelelted方法造成的問題,當細胞被裝入

public partial class TaxaListCellView : MvxTableViewCell 
{ 
    public static readonly UINib Nib = UINib.FromName ("TaxaListCellView", NSBundle.MainBundle); 
    public static readonly NSString Key = new NSString ("TaxaListCellView"); 

    private const string BindingText = "Name Name; IsChecked IsSelected"; 

    public TaxaListCellView() : base(BindingText) 
    { 
     Accessory = UITableViewCellAccessory.Checkmark; 
     SelectionStyle = UITableViewCellSelectionStyle.None; 

    } 

    public TaxaListCellView (IntPtr handle) : base (BindingText,handle) 
    { 
     Accessory = UITableViewCellAccessory.Checkmark; 
     SelectionStyle = UITableViewCellSelectionStyle.None; 
    } 

    public string Name 
    { 
     get { return lblSelectedTaxon.Text; } 
     set { lblSelectedTaxon.Text = value; } 
    } 

    public bool IsChecked 
    { 
     get { return Accessory == UITableViewCellAccessory.Checkmark; } 
     set { Accessory = value ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None; } 
    } 

    public static TaxaListCellView Create() 
    { 
     return (TaxaListCellView)Nib.Instantiate (null, null) [0]; 
    } 
} 

在我TaxaListView類

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     _searchBar = new UISearchBar(new RectangleF(0,0,320, 44)) 
     { 
      AutocorrectionType = UITextAutocorrectionType.Yes,     
     }; 
     _searchBar.SearchButtonClicked += SearchBar_SearchButtonClicked; 
     _searchBar.TextChanged += SearchBarOnTextChanged; 

     var source = new MvxSimpleTableViewSource(TableView, TaxaListCellView.Key, TaxaListCellView.Key); 

     var set = this.CreateBindingSet<TaxaListView, TaxaListViewModel>(); 
     set.Bind (source).To (vm => vm.Taxa); 
     set.Bind (source) 
     .For (s => s.SelectionChangedCommand) 
     .To (vm => vm.ItemSelectedCommand); 

     set.Apply(); 

     TableView.RowHeight = 50; 
     TableView.Source = source; 
     TableView.AllowsSelection = true; 
     TableView.AllowsSelectionDuringEditing = true; 
     TableView.TableHeaderView = _searchBar; 

     TableView.ReloadData(); 
    } 

我綁定selectedChangedCommand,在我的ViewModel類我提高屬性更改事件

private MvxCommand<TaxonViewModel> _itemSelectedCommand; 

    public ICommand ItemSelectedCommand 
    { 
     get 
     { 
      _itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<TaxonViewModel>(DoSelectedItem); 
      return _itemSelectedCommand; 
     } 
    } 

    private void DoSelectedItem(TaxonViewModel item) 
    { 
     Taxa.First(r => r.TaxonId == item.TaxonId).IsSelected = true; 

     RaisePropertyChanged("Taxon"); 
    } 
相關問題