Created
March 22, 2013 19:20
-
-
Save jfoshee/5223962 to your computer and use it in GitHub Desktop.
Implementation of MonoTouch (Xamarin.iOS) UIPickerViewModel for an IList<T>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Unplugged | |
{ | |
public abstract class ListPickerViewModel<TItem> : UIPickerViewModel | |
{ | |
public TItem SelectedItem { get; private set; } | |
IList<TItem> _items; | |
public IList<TItem> Items | |
{ | |
get { return _items; } | |
set { _items = value; Selected(null, 0, 0); } | |
} | |
public ListPickerViewModel() | |
{ | |
} | |
public ListPickerViewModel(IList<TItem> items) | |
{ | |
Items = items; | |
} | |
public override int GetRowsInComponent(UIPickerView picker, int component) | |
{ | |
if (NoItem()) | |
return 1; | |
return Items.Count; | |
} | |
public override string GetTitle(UIPickerView picker, int row, int component) | |
{ | |
if (NoItem(row)) | |
return ""; | |
var item = Items[row]; | |
return GetTitleForItem(item); | |
} | |
public override void Selected(UIPickerView picker, int row, int component) | |
{ | |
if (NoItem(row)) | |
SelectedItem = default(TItem); | |
else | |
SelectedItem = Items[row]; | |
} | |
public override int GetComponentCount(UIPickerView picker) | |
{ | |
return 1; | |
} | |
public virtual string GetTitleForItem(TItem item) | |
{ | |
return item.ToString(); | |
} | |
bool NoItem(int row = 0) | |
{ | |
return Items == null || row >= Items.Count; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, handy snippet. Can I use this in my Pluralsight course?