Browse Source

Added ICommand and use it in Button.

pull/39/head
Steven Kirk 11 years ago
parent
commit
a72e8e243d
  1. 53
      Perspex.Controls/Button.cs
  2. 19
      Perspex.Input/ICommand.cs
  3. 1
      Perspex.Input/Perspex.Input.csproj

53
Perspex.Controls/Button.cs

@ -21,6 +21,12 @@ namespace Perspex.Controls
public static readonly PerspexProperty<ClickMode> ClickModeProperty =
PerspexProperty.Register<Button, ClickMode>("ClickMode");
public static readonly PerspexProperty<ICommand> CommandProperty =
PerspexProperty.Register<Button, ICommand>("Command");
public static readonly PerspexProperty<object> CommandParameterProperty =
PerspexProperty.Register<Button, object>("CommandParameter");
public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategies.Bubble);
@ -28,6 +34,7 @@ namespace Perspex.Controls
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
ClickEvent.AddClassHandler<Button>(x => x.OnClick);
CommandProperty.Changed.Subscribe(CommandChanged);
}
public event EventHandler<RoutedEventArgs> Click
@ -42,6 +49,18 @@ namespace Perspex.Controls
set { this.SetValue(ClickModeProperty, value); }
}
public ICommand Command
{
get { return this.GetValue(CommandProperty); }
set { this.SetValue(CommandProperty, value); }
}
public object CommandParameter
{
get { return this.GetValue(CommandParameterProperty); }
set { this.SetValue(CommandParameterProperty, value); }
}
protected override Size MeasureOverride(Size availableSize)
{
return base.MeasureOverride(availableSize);
@ -54,6 +73,10 @@ namespace Perspex.Controls
protected virtual void OnClick(RoutedEventArgs e)
{
if (this.Command != null)
{
this.Command.Execute(this.CommandParameter);
}
}
protected override void OnPointerPressed(PointerPressEventArgs e)
@ -84,6 +107,36 @@ namespace Perspex.Controls
}
}
private static void CommandChanged(PerspexPropertyChangedEventArgs e)
{
var button = e.Sender as Button;
if (button != null)
{
var oldCommand = e.OldValue as ICommand;
var newCommand = e.NewValue as ICommand;
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= button.CanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged -= button.CanExecuteChanged;
}
button.CanExecuteChanged(button, EventArgs.Empty);
}
}
private void CanExecuteChanged(object sender, EventArgs e)
{
// HACK: Just set the IsEnabled property for the moment. This needs to be changed to
// use IsEnabledCore etc. but it will do for now.
this.IsEnabled = this.Command == null || this.Command.CanExecute(this.CommandParameter);
}
private void RaiseClickEvent()
{
RoutedEventArgs click = new RoutedEventArgs

19
Perspex.Input/ICommand.cs

@ -0,0 +1,19 @@
// -----------------------------------------------------------------------
// <copyright file="ICommand.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Input
{
using System;
public interface ICommand
{
event EventHandler CanExecuteChanged;
bool CanExecute(object parameter);
void Execute(object parameter);
}
}

1
Perspex.Input/Perspex.Input.csproj

@ -60,6 +60,7 @@
<Compile Include="FocusManager.cs" />
<Compile Include="FocusNavigationDirection.cs" />
<Compile Include="ICloseable.cs" />
<Compile Include="ICommand.cs" />
<Compile Include="IFocusManager.cs" />
<Compile Include="IFocusScope.cs" />
<Compile Include="IInputDevice.cs" />

Loading…
Cancel
Save