2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Binding;
23 namespace Tizen.NUI.Components
26 /// The RadioButtonGroup class is used to group together a set of RadioButton control
27 /// It enables the user to select exclusively single radio button of group.
30 /// RadioButtonGroup radioGroup = new RadioButtonGroup();
31 /// RadioButton radio1 = new RadioButton();
32 /// RadioButton radio2 = new RadioButton();
33 /// radioGroup.Add(radio1);
34 /// radioGroup.Add(radio2);
36 /// <since_tizen> 6 </since_tizen>
37 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public class RadioButtonGroup : SelectGroup
42 /// IsGroupHolderProperty
44 [EditorBrowsable(EditorBrowsableState.Never)]
45 public static readonly BindableProperty IsGroupHolderProperty = BindableProperty.CreateAttached("IsGroupHolder", typeof(bool), typeof(View), false, propertyChanged: OnIsGroupHolderChanged);
47 private static readonly BindableProperty RadioButtonGroupProperty = BindableProperty.CreateAttached("RadioButtonGroup", typeof(RadioButtonGroup), typeof(View), null, propertyChanged: OnRadioButtonGroupChanged);
50 /// Construct RadioButtonGroup
52 /// <since_tizen> 6 </since_tizen>
53 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public RadioButtonGroup() : base()
57 EnableMultiSelection = false;
61 /// Gets a RadioButtonGroup.IsGroupHolder property of a view.
63 /// <param name="view">The group holder.</param>
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 public static bool GetIsGroupHolder(View view) => (bool)view.GetValue(IsGroupHolderProperty);
68 /// Sets a RadioButtonGroup.IsGroupHolder property for a view.
70 /// <param name="view">The group holder.</param>
71 /// <param name="value">The value to set.</param>
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public static void SetIsGroupHolder(View view, bool value) => view.SetValue(IsGroupHolderProperty, value, false, true);
76 /// Gets a attached RadioButtonGroup for a view.
78 /// <param name="view">The group holder.</param>
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public static RadioButtonGroup GetRadioButtonGroup(View view) => view.GetValue(RadioButtonGroupProperty) as RadioButtonGroup;
83 /// Get the RadioButton object at the specified index.
85 /// <param name="index">item index</param>
86 /// <returns>RadioButton</returns>
87 /// <since_tizen> 6 </since_tizen>
88 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public RadioButton GetItem(int index)
92 return ItemGroup[index] as RadioButton;
96 /// Get the RadioButton object at the currently selected. If no item selected, returns null.
98 /// <returns>Currently selected radio button</returns>
99 [EditorBrowsable(EditorBrowsableState.Never)]
100 public RadioButton GetSelectedItem()
102 return (SelectedIndex >= 0 && SelectedIndex < ItemGroup.Count) ? ItemGroup[SelectedIndex] as RadioButton : null;
106 /// Add RadioButton to the end of RadioButtonGroup.
108 /// <param name="radio">The RadioButton to be added to the RadioButtonGroup</param>
109 /// <since_tizen> 6 </since_tizen>
110 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
111 [EditorBrowsable(EditorBrowsableState.Never)]
112 public void Add(RadioButton radio)
114 if (null == radio) return;
115 base.AddSelection(radio);
116 radio.ItemGroup = this;
120 /// Remove RadioButton from the RadioButtonGroup.
122 /// <param name="radio">The RadioButton to remove from the RadioButtonGroup</param>
123 /// <since_tizen> 6 </since_tizen>
124 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
125 [EditorBrowsable(EditorBrowsableState.Never)]
126 public void Remove(RadioButton radio)
128 if (null == radio) return;
129 base.RemoveSelection(radio);
130 radio.ItemGroup = null;
133 private static void OnIsGroupHolderChanged(Binding.BindableObject bindable, object oldValue, object newValue)
135 var view = bindable as View;
137 if (view == null) return;
141 view.SetValue(RadioButtonGroupProperty, null, false, true);
145 if (view.GetValue(RadioButtonGroupProperty) == null)
147 view.SetValue(RadioButtonGroupProperty, new RadioButtonGroup(), false, true);
151 private static void OnRadioButtonGroupChanged(Binding.BindableObject bindable, object oldValue, object newValue)
153 var view = bindable as View;
155 if (view == null) return;
157 if (oldValue is RadioButtonGroup oldGroup)
159 view.ChildAdded -= oldGroup.OnChildChanged;
160 view.ChildRemoved -= oldGroup.OnChildChanged;
161 oldGroup.RemoveAll();
164 if (newValue is RadioButtonGroup newGroup)
166 view.ChildAdded += newGroup.OnChildChanged;
167 view.ChildRemoved += newGroup.OnChildChanged;
168 newGroup.OnChildChanged(view, null);
172 private void OnChildChanged(object sender, EventArgs args)
174 if (sender is View view)
177 foreach (var child in view.Children)
178 if (child is RadioButton radioButton)
183 private void RemoveAll()
185 var copied = ItemGroup.ToArray();
186 foreach (var button in copied)
188 Remove(button as RadioButton);