using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Xamarin.Forms; using Aurora.Models.Media; using System.ComponentModel; using System.Collections.Specialized; namespace Aurora.Design.Components.Library { public partial class Library : ContentView { public Library() { InitializeComponent(); this.LibraryDataGrid.ItemSelected += (sender, e) => { this.SelectedItem = e.SelectedItem; }; } #region ItemsSource Property /// /// Bindable Property for the ItemsSource of the datagrid. /// /// /// /// public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(propertyName: "ItemsSource", returnType: typeof(IEnumerable), declaringType: typeof(Library), defaultBindingMode: BindingMode.Default, propertyChanged: (BindableObject bindable, object oldValue, object newValue) => { Library control = bindable as Library; var libraryDataGrid = control.LibraryDataGrid; libraryDataGrid.ItemsSource = newValue as IEnumerable; if (newValue is INotifyPropertyChanged) { var collection = newValue as INotifyCollectionChanged; collection.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs eventArgs) => OnItemSourceCollectionChanged(sender, eventArgs, bindable); } if (oldValue is INotifyPropertyChanged) { var collection = newValue as INotifyCollectionChanged; collection.CollectionChanged -= (object sender, NotifyCollectionChangedEventArgs eventArgs) => OnItemSourceCollectionChanged(sender, eventArgs, bindable); } }); private static void OnItemSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, BindableObject bindable) { if (sender is IEnumerable) { Library control = bindable as Library; var libraryDataGrid = control.LibraryDataGrid; var collection = libraryDataGrid.ItemsSource as IEnumerable; if (e.NewItems != null) { foreach (object obj in e.NewItems) { collection.Concat(new[] { obj }); } } if (e.OldItems != null) { foreach (object obj in e.OldItems) { var list = collection.ToList(); list.Remove(obj); collection = list; } } } } /// /// Backing property for the ItemsSource property. /// /// public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } #endregion ItemsSource Property /// /// Bindable property for the selected item field on the datagrid. /// /// /// /// public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(propertyName: "SelectedItem", returnType: typeof(object), declaringType: typeof(Library), defaultBindingMode: BindingMode.TwoWay); /// /// Backing property for the SelectedItem property. /// /// public object SelectedItem { get { return ((object)GetValue(SelectedItemProperty)); } set { SetValue(SelectedItemProperty, value); } } /// /// Bindable property for the item double clicked command /// /// /// /// public static readonly BindableProperty ItemDoubleClickedProperty = BindableProperty.Create(propertyName: "ItemDoubleClicked", returnType: typeof(Command), declaringType: typeof(Library), propertyChanged: OnDoubleClickPropertyChanged); /// /// Public backing property /// /// public Command ItemDoubleClicked { get { return (Command)GetValue(ItemDoubleClickedProperty); } set { SetValue(ItemDoubleClickedProperty, value); } } /// /// Event handler for double click property. Adds command execute handler. /// /// /// /// private static void OnDoubleClickPropertyChanged(BindableObject bindable, object newValue, object oldValue) { Library control = bindable as Library; var queueDataGrid = control.LibraryDataGrid; if (queueDataGrid.GestureRecognizers.Count > 0) { var gestureRecognizer = queueDataGrid.GestureRecognizers.First(); if (gestureRecognizer is TapGestureRecognizer) { TapGestureRecognizer tap = gestureRecognizer as TapGestureRecognizer; tap.Tapped += (sender, e) => { control.ItemDoubleClicked.Execute(null); }; } } } } }