This commit is contained in:
watsonb8
2019-12-10 15:10:27 -05:00
parent 01736333e9
commit cf05045448
11 changed files with 98 additions and 237 deletions

View File

@ -1,8 +1,11 @@
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
{
@ -29,7 +32,54 @@ namespace Aurora.Design.Components.Library
returnType: typeof(IEnumerable<object>),
declaringType: typeof(Library),
defaultBindingMode: BindingMode.Default,
propertyChanged: OnItemsSourceChanged);
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.
@ -47,19 +97,6 @@ namespace Aurora.Design.Components.Library
}
}
/// <summary>
/// ItemsSource Changed event handler
/// </summary>
/// <param name="bindable"></param>
/// <param name="oldValue"></param>
/// <param name="newValue"></param>
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
Library control = bindable as Library;
var libraryDataGrid = control.LibraryDataGrid;
libraryDataGrid.ItemsSource = newValue as IEnumerable<object>;
}
#endregion ItemsSource Property