88using Xamarin . Forms ;
99using Xamarin . Forms . Internals ;
1010using Xamarin . Forms . Xaml ;
11+ using RadioButton = DIPS . Xamarin . UI . Internal . xaml . RadioButton ;
1112
1213namespace DIPS . Xamarin . UI . Controls . RadioButtonGroup
1314{
@@ -17,8 +18,6 @@ namespace DIPS.Xamarin.UI.Controls.RadioButtonGroup
1718 [ XamlCompilation ( XamlCompilationOptions . Compile ) ]
1819 public partial class RadioButtonGroup : ContentView , IHandleRadioButtons
1920 {
20- private readonly IList < Internal . xaml . RadioButton > m_radioButtons = new List < Internal . xaml . RadioButton > ( ) ;
21-
2221 /// <summary>
2322 /// <see cref="SelectedColor" />
2423 /// </summary>
@@ -48,8 +47,7 @@ public partial class RadioButtonGroup : ContentView, IHandleRadioButtons
4847 nameof ( SeparatorColor ) ,
4948 typeof ( Color ) ,
5049 typeof ( RadioButtonGroup ) ,
51- Color . Black ,
52- BindingMode . OneWay ) ;
50+ Color . Black ) ;
5351
5452 /// <summary>
5553 /// <see cref="ItemsSource" />
@@ -89,6 +87,18 @@ public partial class RadioButtonGroup : ContentView, IHandleRadioButtons
8987 BindingMode . TwoWay ,
9088 propertyChanged : OnIsSelectedPropertyChanged ) ;
9189
90+ /// <summary>
91+ /// <see cref="TextColor" />
92+ /// </summary>
93+ public static readonly BindableProperty TextColorProperty = BindableProperty . Create (
94+ nameof ( TextColor ) ,
95+ typeof ( Color ) ,
96+ typeof ( RadioButtonGroup ) ,
97+ Color . Black ,
98+ propertyChanged : TextColorPropertyChanged ) ;
99+
100+ private readonly IList < RadioButton > m_radioButtons = new List < RadioButton > ( ) ;
101+
92102 /// <summary>
93103 /// Constructs an radio button group
94104 /// </summary>
@@ -97,6 +107,16 @@ public RadioButtonGroup()
97107 InitializeComponent ( ) ;
98108 }
99109
110+ /// <summary>
111+ /// Color of the text in the radiobutton label.
112+ /// This is a bindable property.
113+ /// </summary>
114+ public Color TextColor
115+ {
116+ get => ( Color ) GetValue ( TextColorProperty ) ;
117+ set => SetValue ( TextColorProperty , value ) ;
118+ }
119+
100120 /// <summary>
101121 /// The color for each radio button when it is not selected
102122 /// This is a bindable property
@@ -174,7 +194,7 @@ public Color SeparatorColor
174194 set => SetValue ( SeparatorColorProperty , value ) ;
175195 }
176196
177- void IHandleRadioButtons . OnRadioButtonTapped ( Internal . xaml . RadioButton tappedRadioButton )
197+ void IHandleRadioButtons . OnRadioButtonTapped ( RadioButton tappedRadioButton )
178198 {
179199 if ( SelectedItem == tappedRadioButton . Identifier ) return ;
180200
@@ -185,29 +205,34 @@ void IHandleRadioButtons.OnRadioButtonTapped(Internal.xaml.RadioButton tappedRad
185205 SelectedItem = selectedObject ;
186206 }
187207
208+ private static void TextColorPropertyChanged ( BindableObject bindable , object oldvalue , object newvalue )
209+ {
210+ if ( ! ( bindable is RadioButtonGroup radioButtonGroup ) )
211+ return ;
212+ if ( ! ( newvalue is Color newColor ) )
213+ return ;
214+
215+ radioButtonGroup . m_radioButtons . ForEach ( rb => { rb . TextColor = newColor ; } ) ;
216+ }
217+
188218 private static void OnItemsSourcePropertyChanged ( BindableObject bindable , object oldvalue , object newvalue )
189219 {
190220 if ( ! ( bindable is RadioButtonGroup radioButtonGroup ) ) return ;
191221
192222 var oldNotifyCollectionChanged = oldvalue as INotifyCollectionChanged ;
193- if ( oldNotifyCollectionChanged != null )
194- {
195- oldNotifyCollectionChanged . CollectionChanged -= radioButtonGroup . CollectionChanged ;
196- }
223+ if ( oldNotifyCollectionChanged != null ) oldNotifyCollectionChanged . CollectionChanged -= radioButtonGroup . CollectionChanged ;
197224
198225 var newItemsSource = newvalue as IEnumerable ;
199226 if ( newItemsSource == null ) return ;
200227
201228 if ( newItemsSource is INotifyCollectionChanged newNotifyCollectionChanged )
202- {
203229 newNotifyCollectionChanged . CollectionChanged += radioButtonGroup . CollectionChanged ;
204- }
205230
206231 radioButtonGroup . Initialize ( newItemsSource ) ;
207232 }
208233
209234 private void CollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
210- {
235+ {
211236 Initialize ( ItemsSource ) ;
212237 }
213238
@@ -247,8 +272,8 @@ private void Initialize(IEnumerable newItems)
247272 var items = newItems . Cast < object > ( ) . ToArray ( ) ;
248273 if ( items . Length == 0 ) return ;
249274
250- AddSeparator ( ) ;
251-
275+ AddSeparator ( ) ;
276+
252277 foreach ( var item in newItems )
253278 {
254279 AddItem ( item ) ;
@@ -259,31 +284,28 @@ private void Initialize(IEnumerable newItems)
259284 {
260285 var selectedRadioButton = m_radioButtons . FirstOrDefault ( rb => rb . Identifier == SelectedItem ) ;
261286 if ( selectedRadioButton != null )
262- {
263287 selectedRadioButton . IsSelected = true ;
264- }
265288 else
266- {
267289 SelectedItem = null ;
268- }
269290 }
270291 }
271292
272- private void AddItem ( object item )
273- {
274- var radioButton = new Internal . xaml . RadioButton
275- {
276- Text = item . GetPropertyValue ( DisplayMemberPath ) ,
277- Identifier = item ,
278- SelectedColor = SelectedColor ,
279- DeSelectedColor = DeSelectedColor ,
280- Padding = new Thickness ( 0 , 15 , 0 , 15 ) ,
281- } ;
282-
293+ private void AddItem ( object item )
294+ {
295+ var radioButton = new RadioButton
296+ {
297+ TextColor = TextColor ,
298+ Text = item . GetPropertyValue ( DisplayMemberPath ) ,
299+ Identifier = item ,
300+ SelectedColor = SelectedColor ,
301+ DeSelectedColor = DeSelectedColor ,
302+ Padding = new Thickness ( 0 , 15 , 0 , 15 )
303+ } ;
304+
283305 radioButton . RefreshColor ( radioButton . IsSelected ) ;
284- radioButton . Initialize ( this ) ;
285- m_radioButtons . Add ( radioButton ) ;
286- AddChild ( radioButton ) ;
306+ radioButton . Initialize ( this ) ;
307+ m_radioButtons . Add ( radioButton ) ;
308+ AddChild ( radioButton ) ;
287309 }
288310
289311 private void AddSeparator ( )
@@ -293,9 +315,9 @@ private void AddSeparator()
293315 AddChild ( separator ) ;
294316 }
295317
296- private void AddChild ( View view )
318+ private void AddChild ( View view )
297319 {
298- container . Children . Add ( view ) ;
320+ container . Children . Add ( view ) ;
299321 }
300322
301323 private static void OnIsSelectedPropertyChanged ( BindableObject bindable , object oldvalue , object newvalue )
0 commit comments