Skip to content

Commit

Permalink
Merge pull request #3186 from MahApps/feature/WpfAnalyzers
Browse files Browse the repository at this point in the history
Analyse code with DotNetAnalyzers/WpfAnalyzers
  • Loading branch information
punker76 committed Feb 5, 2018
2 parents 23e8bb1 + 978a265 commit 3a7920a
Show file tree
Hide file tree
Showing 34 changed files with 360 additions and 205 deletions.
@@ -1,8 +1,10 @@
<controls:Flyout x:Class="MetroDemo.ExampleWindows.ShowcaseFlyout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:actions="clr-namespace:MahApps.Metro.Actions;assembly=MahApps.Metro"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:metroDemo="clr-namespace:MetroDemo"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Expand Down Expand Up @@ -39,15 +41,18 @@
Height="34"
Margin="2 4 2 2"
VerticalAlignment="Bottom"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:Flyout}}, Path=InternalCloseCommand, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:Flyout}}, Path=CloseCommandParameter, Mode=OneWay}"
DockPanel.Dock="Left"
FontFamily="Segoe UI Symbol"
FontSize="16"
Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:Flyout}}, Path=Foreground}"
IsCancel="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:Flyout}}, Path=CloseButtonIsCancel}"
Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}"
Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:Flyout}}, Path=CloseButtonVisibility}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<actions:CloseFlyoutAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:Flyout}}, Path=CloseCommand, Mode=OneWay}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:Flyout}}, Path=CloseCommandParameter, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ContentControl Width="20"
Height="20"
Content="M19,34V42H43.75L33.75,52H44.25L58.25,38L44.25,24H33.75L43.75,34H19Z"
Expand Down
@@ -0,0 +1,38 @@
using MahApps.Metro.Controls;

namespace MahApps.Metro.Actions
{
public class CloseFlyoutAction : CommandTriggerAction
{
private Flyout associatedFlyout;

private Flyout AssociatedFlyout => this.associatedFlyout ?? (this.associatedFlyout = this.AssociatedObject.TryFindParent<Flyout>());

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);
}
}
else
{
this.AssociatedFlyout?.SetCurrentValue(Flyout.IsOpenProperty, false);
}
}

protected override object GetCommandParameter()
{
return this.CommandParameter ?? this.AssociatedFlyout;
}
}
}
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
@@ -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();
}
}
}

0 comments on commit 3a7920a

Please sign in to comment.