2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using System.Windows.Input;
22 namespace Tizen.Wearable.CircularUI.Forms
25 /// The ContentButton is a Button, which allows you to customize the View to be displayed.
27 /// <since_tizen> 4 </since_tizen>
28 public class ContentButton : ContentView, IButtonController
31 /// BindableProperty. Identifies the Command bindable property.
33 /// <since_tizen> 4 </since_tizen>
34 public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ContentButton), null,
35 propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
38 /// BindableProperty. Identifies the CommandParameter bindable property.
40 /// <since_tizen> 4 </since_tizen>
41 public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(ContentButton), null,
42 propertyChanged: (bindable, oldvalue, newvalue) => CommandCanExcuteChanged(bindable, EventArgs.Empty));
45 /// Gets or sets command that is executed when the button is clicked.
47 /// <since_tizen> 4 </since_tizen>
48 public ICommand Command
50 get => (ICommand)GetValue(CommandProperty);
51 set => SetValue(CommandProperty, value);
55 /// Gets or sets command paramter that is executed when the button is clicked.
57 /// <since_tizen> 4 </since_tizen>
58 public object CommandParameter
60 get => GetValue(CommandParameterProperty);
61 set => SetValue(CommandParameterProperty, value);
65 /// Occurs when the button is clicked.
67 /// <since_tizen> 4 </since_tizen>
68 public event EventHandler Clicked;
71 /// Occurs when the button is pressed.
73 /// <since_tizen> 4 </since_tizen>
74 public event EventHandler Pressed;
77 /// Occurs when the button is released.
79 /// <since_tizen> 4 </since_tizen>
80 public event EventHandler Released;
84 set => SetValueCore(IsEnabledProperty, value);
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public void SendClicked()
95 Command?.Execute(CommandParameter);
96 Clicked?.Invoke(this, EventArgs.Empty);
101 /// For internal use.
103 [EditorBrowsable(EditorBrowsableState.Never)]
104 public void SendPressed()
108 Pressed?.Invoke(this, EventArgs.Empty);
113 /// For internal use.
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public void SendReleased()
120 Released?.Invoke(this, EventArgs.Empty);
124 protected override void OnBindingContextChanged()
126 base.OnBindingContextChanged();
128 View content = Content;
131 SetInheritedBindingContext(content, BindingContext);
135 static void OnCommandChanged(BindableObject bindable, object oldCommand, object newCommand)
137 ContentButton button = (ContentButton)bindable;
138 if (newCommand is ICommand command)
140 command.CanExecuteChanged += button.OnCommandCanExecuteChanged;
142 CommandChanged(button);
145 static void CommandChanged(ContentButton button)
147 if(button.Command != null)
149 CommandCanExcuteChanged(button, EventArgs.Empty);
153 button.IsEnabledCore = true;
157 static void OnCommandChanging(BindableObject bindable, object oldCommand, object newCommand)
159 ContentButton button = (ContentButton)bindable;
160 if (oldCommand != null)
162 (oldCommand as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
166 static void CommandCanExcuteChanged(object sender, EventArgs e)
168 var button = (ContentButton)sender;
169 if (button.Command != null)
171 button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);
175 void OnCommandCanExecuteChanged(object sender, EventArgs e)
177 ContentButton button = (ContentButton)sender;
178 if (button.Command != null)
180 button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);