Skip to content

Commit

Permalink
Add new CommandTriggerAction
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Feb 5, 2018
1 parent a0356d0 commit 2d3464d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,16 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using MahApps.Metro.Controls;

namespace MahApps.Metro.Actions
{
public class CloseTabItemAction : TriggerAction<FrameworkElement>
public class CloseTabItemAction : CommandTriggerAction
{
private TabItem associatedTabItem;

private TabItem AssociatedTabItem => this.associatedTabItem ?? (this.associatedTabItem = this.AssociatedObject.TryFindParent<TabItem>());

/// <summary>
/// Identifies the <see cref="Command" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandProperty
= DependencyProperty.Register(nameof(Command),
typeof(ICommand),
typeof(CloseTabItemAction),
new PropertyMetadata(null, (s, e) => OnCommandChanged(s as CloseTabItemAction, e)));

/// <summary>
/// Gets or sets the command that this trigger is bound to.
/// </summary>
public ICommand Command
{
get { return (ICommand)this.GetValue(CommandProperty); }
set { this.SetValue(CommandProperty, value); }
}

/// <summary>
/// Identifies the <see cref="CommandParameter" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandParameterProperty
= DependencyProperty.Register(nameof(CommandParameter),
typeof(object),
typeof(CloseTabItemAction),
new PropertyMetadata(null,
(s, e) =>
{
var sender = s as CloseTabItemAction;
if (sender?.AssociatedObject != null)
{
sender.EnableDisableElement();
}
}));

/// <summary>
/// Gets or sets an object that will be passed to the <see cref="Command" /> attached to this trigger.
/// </summary>
public object CommandParameter
{
get { return this.GetValue(CommandParameterProperty); }
set { this.SetValue(CommandParameterProperty, value); }
}

[Obsolete("This property will be deleted in the next release.")]
public static readonly DependencyProperty TabControlProperty =
DependencyProperty.Register(nameof(TabControl),
Expand Down Expand Up @@ -87,12 +41,6 @@ public TabItem TabItem
set { this.SetValue(TabItemProperty, value); }
}

protected override void OnAttached()
{
base.OnAttached();
this.EnableDisableElement();
}

protected override void Invoke(object parameter)
{
if (this.AssociatedObject == null || (this.AssociatedObject != null && !this.AssociatedObject.IsEnabled))
Expand Down Expand Up @@ -160,46 +108,9 @@ protected override void Invoke(object parameter)
}
}

private static void OnCommandChanged(CloseTabItemAction action, DependencyPropertyChangedEventArgs e)
{
if (action == null)
{
return;
}

if (e.OldValue != null)
{
((ICommand)e.OldValue).CanExecuteChanged -= action.OnCommandCanExecuteChanged;
}

var command = (ICommand)e.NewValue;
if (command != null)
{
command.CanExecuteChanged += action.OnCommandCanExecuteChanged;
}

action.EnableDisableElement();
}

private object GetCommandParameter()
protected override object GetCommandParameter()
{
return this.CommandParameter ?? this.AssociatedTabItem;
}

private void EnableDisableElement()
{
if (this.AssociatedObject == null)
{
return;
}

var command = this.Command;
this.AssociatedObject.IsEnabled = command == null || command.CanExecute(this.GetCommandParameter());
}

private void OnCommandCanExecuteChanged(object sender, EventArgs e)
{
this.EnableDisableElement();
}
}
}
128 changes: 128 additions & 0 deletions src/MahApps.Metro/MahApps.Metro.Shared/Actions/CommandTriggerAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace MahApps.Metro.Actions
{
/// <summary>
/// This CommandTriggerAction can be used to bind any event on any FrameworkElement to an <see cref="ICommand" />.
/// This trigger can only be attached to a FrameworkElement or a class deriving from FrameworkElement.
///
/// This class is inspired from Laurent Bugnion and his EventToCommand.
/// <web>http://www.mvvmlight.net</web>
/// <license> See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt </license>
/// </summary>
public class CommandTriggerAction : TriggerAction<FrameworkElement>
{
/// <summary>
/// Identifies the <see cref="Command" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandProperty
= DependencyProperty.Register(nameof(Command),
typeof(ICommand),
typeof(CommandTriggerAction),
new PropertyMetadata(null, (s, e) => OnCommandChanged(s as CommandTriggerAction, e)));

/// <summary>
/// Gets or sets the command that this trigger is bound to.
/// </summary>
public ICommand Command
{
get { return (ICommand)this.GetValue(CommandProperty); }
set { this.SetValue(CommandProperty, value); }
}

/// <summary>
/// Identifies the <see cref="CommandParameter" /> dependency property
/// </summary>
public static readonly DependencyProperty CommandParameterProperty
= DependencyProperty.Register(nameof(CommandParameter),
typeof(object),
typeof(CommandTriggerAction),
new PropertyMetadata(null,
(s, e) =>
{
var sender = s as CommandTriggerAction;
if (sender?.AssociatedObject != null)
{
sender.EnableDisableElement();
}
}));

/// <summary>
/// Gets or sets an object that will be passed to the <see cref="Command" /> attached to this trigger.
/// </summary>
public object CommandParameter
{
get { return this.GetValue(CommandParameterProperty); }
set { this.SetValue(CommandParameterProperty, value); }
}

protected override void OnAttached()
{
base.OnAttached();
this.EnableDisableElement();
}

protected override void Invoke(object parameter)
{
if (this.AssociatedObject == null || (this.AssociatedObject != null && !this.AssociatedObject.IsEnabled))
{
return;
}

var command = this.Command;
if (command != null)
{
var commandParameter = this.GetCommandParameter();
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
}

private static void OnCommandChanged(CommandTriggerAction action, DependencyPropertyChangedEventArgs e)
{
if (action == null)
{
return;
}

if (e.OldValue != null)
{
((ICommand)e.OldValue).CanExecuteChanged -= action.OnCommandCanExecuteChanged;
}

var command = (ICommand)e.NewValue;
if (command != null)
{
command.CanExecuteChanged += action.OnCommandCanExecuteChanged;
}

action.EnableDisableElement();
}

protected virtual object GetCommandParameter()
{
return this.CommandParameter ?? this.AssociatedObject;
}

private void EnableDisableElement()
{
if (this.AssociatedObject == null)
{
return;
}

var command = this.Command;
this.AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, command == null || command.CanExecute(this.GetCommandParameter()));
}

private void OnCommandCanExecuteChanged(object sender, EventArgs e)
{
this.EnableDisableElement();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Actions\CloseTabItemAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Actions\CommandTriggerAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Actions\SetFlyoutOpenAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviours\BindableResourceBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviours\BorderlessWindowBehavior.cs" />
Expand Down

0 comments on commit 2d3464d

Please sign in to comment.