using System.Windows.Input; namespace gehGassi.Toolkit.Views.ContentButton { /// /// Button der einen beliebigen Inhalt haben kann /// [ContentProperty(nameof(Content))] public class ContentButton : ContentView, IContentButton { private const string PressedState = "Pressed"; /// /// Event der ausgelöst wird, wenn der Button geklickt wird /// public event EventHandler Clicked; /// /// Event der ausgelöst wird, wenn der Button gedrückt wird /// public event EventHandler Pressed; /// /// Event der ausgelöst wird wenn der Button losgelassen wird /// public event EventHandler Released; public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ContentButton), defaultValue: null); public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(ContentButton), defaultValue: null); /// /// Command das bei Klick ausgeführt werden soll /// public virtual ICommand Command { get => (ICommand)GetValue(CommandProperty); set => SetValue(CommandProperty, value); } /// /// Optinaler Command-Parameter /// public virtual object CommandParameter { get => GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); } /// /// Methode die aufgerufen wird wenn der Button geklickt wird /// public virtual void OnClicked() { Clicked?.Invoke(this, EventArgs.Empty); if (Command?.CanExecute(CommandParameter) == true) { Command?.Execute(CommandParameter); } } /// /// Methode die aufgerufen wird wenn der Button gedrückt wird /// public virtual void OnPressed(Point pressPosition) { VisualStateManager.GoToState(this, PressedState); Pressed?.Invoke(this, new ContentButtonEventArgs { InteractionPosition = pressPosition }); } /// /// Methode die aufgerufen wurde wenn der Button losgelassen wird /// public virtual void OnReleased(Point releasePosition) { GoToDefaultState(); Released?.Invoke(this, new ContentButtonEventArgs { InteractionPosition = releasePosition }); } /// /// Zum Standard-Status des Buttons wechseln /// private void GoToDefaultState() { if (!IsEnabled) VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Disabled); else if (IsFocused) VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Focused); else VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal); } } }