This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-sharp-desktop/Aurora/Design/Components/Library/Library.xaml.cs

186 lines
6.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-12-10 20:10:27 +00:00
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;
2019-07-05 18:17:09 +00:00
using Aurora.Models.Media;
2019-12-10 20:10:27 +00:00
using System.ComponentModel;
using System.Collections.Specialized;
2019-12-01 11:53:30 +00:00
namespace Aurora.Design.Components.Library
{
2019-12-01 11:53:30 +00:00
public partial class Library : ContentView
{
2019-12-01 11:53:30 +00:00
public Library()
{
InitializeComponent();
2019-12-01 11:53:30 +00:00
this.LibraryDataGrid.ItemSelected += (sender, e) =>
2019-11-04 04:17:34 +00:00
{
this.SelectedItem = e.SelectedItem;
};
}
#region ItemsSource Property
/// <summary>
/// Bindable Property for the ItemsSource of the datagrid.
/// </summary>
/// <param name=""ItemsSource""></param>
/// <param name="typeof(IEnumerable<object>"></param>
/// <returns></returns>
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create(propertyName: "ItemsSource",
returnType: typeof(IEnumerable<object>),
2019-12-01 11:53:30 +00:00
declaringType: typeof(Library),
defaultBindingMode: BindingMode.Default,
2019-12-10 20:10:27 +00:00
propertyChanged: (BindableObject bindable, object oldValue, object newValue) =>
{
Library control = bindable as Library;
var libraryDataGrid = control.LibraryDataGrid;
libraryDataGrid.ItemsSource = newValue as IEnumerable<object>;
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<object>)
{
Library control = bindable as Library;
var libraryDataGrid = control.LibraryDataGrid;
var collection = libraryDataGrid.ItemsSource as IEnumerable<object>;
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;
}
}
}
}
/// <summary>
/// Backing property for the ItemsSource property.
/// </summary>
/// <value></value>
public IEnumerable<object> ItemsSource
{
get
{
return (IEnumerable<object>)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
}
}
#endregion ItemsSource Property
/// <summary>
/// Bindable property for the selected item field on the datagrid.
/// </summary>
/// <param name=""SelectedItem""></param>
/// <param name="typeof(BaseMetadata"></param>
/// <returns></returns>
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create(propertyName: "SelectedItem",
returnType: typeof(object),
2019-12-01 11:53:30 +00:00
declaringType: typeof(Library),
2019-11-04 04:17:34 +00:00
defaultBindingMode: BindingMode.TwoWay);
/// <summary>
/// Backing property for the SelectedItem property.
/// </summary>
/// <value></value>
public object SelectedItem
{
get
{
return ((object)GetValue(SelectedItemProperty));
}
set
{
SetValue(SelectedItemProperty, value);
}
}
2019-11-04 04:17:34 +00:00
/// <summary>
/// Bindable property for the item double clicked command
/// </summary>
/// <param name=""SelectedItem""></param>
/// <param name="typeof(BaseMetadata"></param>
/// <returns></returns>
public static readonly BindableProperty ItemDoubleClickedProperty =
BindableProperty.Create(propertyName: "ItemDoubleClicked",
returnType: typeof(Command),
2019-12-01 11:53:30 +00:00
declaringType: typeof(Library),
2019-11-04 04:17:34 +00:00
propertyChanged: OnDoubleClickPropertyChanged);
/// <summary>
/// Public backing property
/// </summary>
/// <value></value>
public Command ItemDoubleClicked
{
get
{
return (Command)GetValue(ItemDoubleClickedProperty);
}
set
{
SetValue(ItemDoubleClickedProperty, value);
}
}
/// <summary>
2019-11-04 04:17:34 +00:00
/// Event handler for double click property. Adds command execute handler.
/// </summary>
2019-11-04 04:17:34 +00:00
/// <param name="bindable"></param>
/// <param name="newValue"></param>
/// <param name="oldValue"></param>
2019-11-04 04:17:34 +00:00
private static void OnDoubleClickPropertyChanged(BindableObject bindable, object newValue, object oldValue)
{
2019-12-01 11:53:30 +00:00
Library control = bindable as Library;
var queueDataGrid = control.LibraryDataGrid;
2019-11-04 04:17:34 +00:00
if (queueDataGrid.GestureRecognizers.Count > 0)
{
2019-11-04 04:17:34 +00:00
var gestureRecognizer = queueDataGrid.GestureRecognizers.First();
2019-11-04 04:17:34 +00:00
if (gestureRecognizer is TapGestureRecognizer)
{
TapGestureRecognizer tap = gestureRecognizer as TapGestureRecognizer;
tap.Tapped += (sender, e) =>
{
control.ItemDoubleClicked.Execute(null);
};
}
}
}
}
}