Skip to content

Commit 6cb4854

Browse files
committed
Added textcolor property
1 parent 53baa1c commit 6cb4854

File tree

5 files changed

+76
-45
lines changed

5 files changed

+76
-45
lines changed

src/DIPS.Xamarin.UI/Controls/RadioButtonGroup/RadioButtonGroup.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
<StackLayout x:Name="container"
1111
Orientation="Vertical"
1212
HorizontalOptions="{Binding Source={x:Reference codeBehind}, Path=HorizontalOptions}"
13-
VerticalOptions="{Binding Source={x:Reference codeBehind}, Path=VerticalOptions}"
14-
/>
13+
VerticalOptions="{Binding Source={x:Reference codeBehind}, Path=VerticalOptions}"/>
1514
</ContentView>

src/DIPS.Xamarin.UI/Controls/RadioButtonGroup/RadioButtonGroup.xaml.cs

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Xamarin.Forms;
99
using Xamarin.Forms.Internals;
1010
using Xamarin.Forms.Xaml;
11+
using RadioButton = DIPS.Xamarin.UI.Internal.xaml.RadioButton;
1112

1213
namespace 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)

src/DIPS.Xamarin.UI/Internal/Xaml/RadioButton.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
HeightRequest="{Binding Source={x:Reference outerButton}, Path=HeightRequest}"
3939
VerticalOptions="Center"
4040
Text="{Binding Text}"
41+
TextColor="{Binding TextColor}"
4142
FontSize="Body"
4243
InputTransparent="True"
4344
Margin="{StaticResource spacing}" />

src/DIPS.Xamarin.UI/Internal/Xaml/RadioButton.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ public string Text
128128
set => SetValue(TextProperty, value);
129129
}
130130

131+
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(RadioButton), Color.Black);
132+
133+
public Color TextColor
134+
{
135+
get => (Color)GetValue(TextColorProperty);
136+
set => SetValue(TextColorProperty, value);
137+
}
138+
131139
internal void Initialize(IHandleRadioButtons radioButtonsHandler)
132140
{
133141
m_radioButtonsHandler = radioButtonsHandler;

src/Samples/DIPS.Xamarin.UI.Samples/Controls/RadioButtonGroup/RadioButtonGroupPage.xaml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
<RowDefinition Height="Auto" />
1919
</Grid.RowDefinitions>
2020

21-
<ScrollView>
22-
<dxui:RadioButtonGroup Grid.Row="0"
23-
ItemsSource="{Binding Items}"
24-
SelectedItem="{Binding SelectedItem}"
25-
SelectedItemChangedCommand="{Binding SelectedItemChangedCommand}"
26-
DisplayMemberPath="Name"
27-
SelectedColor="{Binding SelectedColor}"
28-
DeSelectedColor="{Binding DeSelectedColor}"
29-
SeparatorColor="{Binding SeparatorColor}" />
21+
<ScrollView
22+
BackgroundColor="White">
23+
<dxui:RadioButtonGroup ItemsSource="{Binding Items}"
24+
SelectedItem="{Binding SelectedItem}"
25+
SelectedItemChangedCommand="{Binding SelectedItemChangedCommand}"
26+
DisplayMemberPath="Name"
27+
SelectedColor="{Binding SelectedColor}"
28+
DeSelectedColor="{Binding DeSelectedColor}"
29+
SeparatorColor="{Binding SeparatorColor}"
30+
TextColor="White"/>
3031
</ScrollView>
3132
<ScrollView Grid.Row="1">
3233
<StackLayout>

0 commit comments

Comments
 (0)