Initial commit.
This commit is contained in:
202
Wino.Mail/Behaviors/BindableCommandBarBehavior.cs
Normal file
202
Wino.Mail/Behaviors/BindableCommandBarBehavior.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using Microsoft.Xaml.Interactivity;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows.Input;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Controls.Primitives;
|
||||
using Wino.Controls;
|
||||
using Wino.Core.Domain.Models.Menus;
|
||||
using Wino.Helpers;
|
||||
|
||||
namespace Wino.Behaviors
|
||||
{
|
||||
public class BindableCommandBarBehavior : Behavior<CommandBar>
|
||||
{
|
||||
public static readonly DependencyProperty PrimaryCommandsProperty = DependencyProperty.Register(
|
||||
"PrimaryCommands", typeof(object), typeof(BindableCommandBarBehavior),
|
||||
new PropertyMetadata(null, UpdateCommands));
|
||||
|
||||
public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register(
|
||||
"ItemTemplateSelector", typeof(DataTemplateSelector), typeof(BindableCommandBarBehavior),
|
||||
new PropertyMetadata(null, null));
|
||||
|
||||
public ICommand ItemClickedCommand
|
||||
{
|
||||
get { return (ICommand)GetValue(ItemClickedCommandProperty); }
|
||||
set { SetValue(ItemClickedCommandProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ItemClickedCommandProperty = DependencyProperty.Register(nameof(ItemClickedCommand), typeof(ICommand), typeof(BindableCommandBarBehavior), new PropertyMetadata(null));
|
||||
|
||||
public DataTemplateSelector ItemTemplateSelector
|
||||
{
|
||||
get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); }
|
||||
set { SetValue(ItemTemplateSelectorProperty, value); }
|
||||
}
|
||||
|
||||
public object PrimaryCommands
|
||||
{
|
||||
get { return GetValue(PrimaryCommandsProperty); }
|
||||
set { SetValue(PrimaryCommandsProperty, value); }
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
base.OnDetaching();
|
||||
|
||||
AttachChanges(false);
|
||||
|
||||
if (PrimaryCommands is IEnumerable enumerable)
|
||||
{
|
||||
foreach (var item in enumerable)
|
||||
{
|
||||
if (item is ButtonBase button)
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePrimaryCommands()
|
||||
{
|
||||
if (AssociatedObject == null)
|
||||
return;
|
||||
|
||||
if (PrimaryCommands == null)
|
||||
return;
|
||||
|
||||
if (AssociatedObject.PrimaryCommands is IEnumerable enumerableObjects)
|
||||
{
|
||||
foreach (var item in enumerableObjects)
|
||||
{
|
||||
if (item is ButtonBase button)
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (AssociatedObject.SecondaryCommands is IEnumerable secondaryObject)
|
||||
{
|
||||
foreach (var item in secondaryObject)
|
||||
{
|
||||
if (item is ButtonBase button)
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AssociatedObject.PrimaryCommands.Clear();
|
||||
AssociatedObject.SecondaryCommands.Clear();
|
||||
|
||||
if (!(PrimaryCommands is IEnumerable enumerable)) return;
|
||||
|
||||
foreach (var command in enumerable)
|
||||
{
|
||||
if (command is MailOperationMenuItem mailOperationMenuItem)
|
||||
{
|
||||
if (mailOperationMenuItem.Operation == Core.Domain.Enums.MailOperation.Seperator)
|
||||
{
|
||||
var seperator = new AppBarSeparator();
|
||||
|
||||
if (mailOperationMenuItem.IsSecondaryMenuPreferred)
|
||||
{
|
||||
AssociatedObject.SecondaryCommands.Add(seperator);
|
||||
}
|
||||
else
|
||||
{
|
||||
AssociatedObject.PrimaryCommands.Add(seperator);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var menuItem = new AppBarButton()
|
||||
{
|
||||
Icon = new WinoFontIcon() { Glyph = ControlConstants.WinoIconFontDictionary[XamlHelpers.GetWinoIconGlyph(mailOperationMenuItem.Operation)] },
|
||||
Label = XamlHelpers.GetOperationString(mailOperationMenuItem.Operation),
|
||||
DataContext = mailOperationMenuItem,
|
||||
};
|
||||
|
||||
menuItem.Click -= Button_Click;
|
||||
menuItem.Click += Button_Click;
|
||||
|
||||
if (mailOperationMenuItem.IsSecondaryMenuPreferred)
|
||||
{
|
||||
AssociatedObject.SecondaryCommands.Add(menuItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
AssociatedObject.PrimaryCommands.Add(menuItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if (dependencyObject is ICommandBarElement icommandBarElement)
|
||||
//{
|
||||
// if (dependencyObject is ButtonBase button)
|
||||
// {
|
||||
// button.Click -= Button_Click;
|
||||
// button.Click += Button_Click;
|
||||
// }
|
||||
|
||||
// if (command is MailOperationMenuItem mailOperationMenuItem)
|
||||
// {
|
||||
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ItemClickedCommand?.Execute(((ButtonBase)sender).DataContext);
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
|
||||
AttachChanges(true);
|
||||
|
||||
UpdatePrimaryCommands();
|
||||
}
|
||||
|
||||
private void PrimaryCommandsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
UpdatePrimaryCommands();
|
||||
}
|
||||
|
||||
private static void UpdateCommands(DependencyObject dependencyObject,
|
||||
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
|
||||
{
|
||||
if (!(dependencyObject is BindableCommandBarBehavior behavior)) return;
|
||||
|
||||
if (dependencyPropertyChangedEventArgs.OldValue is INotifyCollectionChanged oldList)
|
||||
{
|
||||
oldList.CollectionChanged -= behavior.PrimaryCommandsCollectionChanged;
|
||||
}
|
||||
|
||||
behavior.AttachChanges(true);
|
||||
behavior.UpdatePrimaryCommands();
|
||||
}
|
||||
|
||||
private void AttachChanges(bool register)
|
||||
{
|
||||
if (PrimaryCommands is null) return;
|
||||
|
||||
if (PrimaryCommands is INotifyCollectionChanged collectionChanged)
|
||||
{
|
||||
if (register)
|
||||
{
|
||||
collectionChanged.CollectionChanged -= PrimaryCommandsCollectionChanged;
|
||||
collectionChanged.CollectionChanged += PrimaryCommandsCollectionChanged;
|
||||
}
|
||||
else
|
||||
collectionChanged.CollectionChanged -= PrimaryCommandsCollectionChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user