Skip to content

Instantly share code, notes, and snippets.

@chrisriesgo
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisriesgo/163bf2ec6fa6956784c6 to your computer and use it in GitHub Desktop.
Save chrisriesgo/163bf2ec6fa6956784c6 to your computer and use it in GitHub Desktop.
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create<CarouselLayout, IList> (
view => view.ItemsSource,
null,
propertyChanging: (bindableObject, oldValue, newValue) => {
((CarouselLayout)bindableObject).ItemsSourceChanging ();
},
propertyChanged: (bindableObject, oldValue, newValue) => {
((CarouselLayout)bindableObject).ItemsSourceChanged ();
}
);
public IList ItemsSource {
get {
return (IList)GetValue (ItemsSourceProperty);
}
set {
SetValue (ItemsSourceProperty, value);
}
}
void ItemsSourceChanging ()
{
if (ItemsSource == null) return;
_selectedIndex = ItemsSource.IndexOf (SelectedItem);
}
void ItemsSourceChanged ()
{
_stack.Children.Clear ();
foreach (var item in ItemsSource) {
var view = (View)ItemTemplate.CreateContent ();
var bindableObject = view as BindableObject;
if (bindableObject != null)
bindableObject.BindingContext = item;
_stack.Children.Add (view);
}
if (_selectedIndex >= 0) SelectedIndex = _selectedIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment