Skip to content

Commit 870bc3e

Browse files
Segment control changes added
1 parent 082ad8a commit 870bc3e

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

maui/src/SegmentedControl/Helper/SegmentViewHelper.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,20 @@ internal static Color GetSelectedSegmentForeground(ISegmentItemInfo? itemInfo, S
280280
{
281281
return segmentItem.SelectedSegmentTextColor ?? itemInfo?.SelectionIndicatorSettings?.TextColor ?? Colors.White;
282282
}
283+
else
284+
{
285+
Color textColor;
286+
if (itemInfo != null && itemInfo.SelectionIndicatorSettings != null && !itemInfo.SelectedSegmentTextColor.Equals(itemInfo.SelectionIndicatorSettings.TextColor))
287+
{
288+
textColor = itemInfo.SelectionIndicatorSettings.TextColor;
289+
}
290+
else
291+
{
292+
textColor = BrushToColorConverter(GetSelectedSegmentStroke(itemInfo, segmentItem));
293+
}
283294

284-
// If the selection indicator is not filled, get the text color from the selected segment background.
285-
Brush textColor = GetSelectedSegmentStroke(itemInfo, segmentItem);
286-
return BrushToColorConverter(textColor);
295+
return segmentItem.SelectedSegmentTextColor ?? textColor;
296+
}
287297
}
288298

289299
/// <summary>

maui/src/SegmentedControl/Interface/ISegmentInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ internal interface ISegmentInfo
8787
/// </summary>
8888
Brush KeyboardFocusStroke { get; }
8989

90+
/// <summary>
91+
/// Gets the selected text color for the segment.
92+
/// </summary>
93+
Color SelectedSegmentTextColor { get; }
94+
9095
/// <summary>
9196
/// Gets the background brush for the segment when it is disabled.
9297
/// </summary>

maui/src/SegmentedControl/Model/SfSegmentedControl.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ public partial class SfSegmentedControl
290290
typeof(SfSegmentedControl),
291291
defaultValueCreator: bindable => null);
292292

293+
/// <summary>
294+
/// Identifies the <see cref="SelectedSegmentTextColor"/> dependency property.
295+
/// </summary>
296+
/// <value>
297+
/// Identifies the <see cref="SelectedSegmentTextColor"/> bindable property.
298+
/// </value>
299+
internal static readonly BindableProperty SelectedSegmentTextColorProperty =
300+
BindableProperty.Create(
301+
nameof(SelectedSegmentTextColor),
302+
typeof(Color), typeof(SfSegmentedControl),
303+
defaultValueCreator: bindable => Color.FromArgb("#FFFFFF"),
304+
propertyChanged: OnSelectedSegmentTextColorChanged);
305+
293306
/// <summary>
294307
/// Identifies the <see cref="KeyboardFocusStroke"/> dependency property.
295308
/// </summary>
@@ -993,6 +1006,15 @@ internal Brush HoveredBackground
9931006
set { SetValue(HoveredBackgroundProperty, value); }
9941007
}
9951008

1009+
/// <summary>
1010+
/// Gets or sets the selected text color brush for the segment.
1011+
/// </summary>
1012+
internal Color SelectedSegmentTextColor
1013+
{
1014+
get { return (Color)this.GetValue(SelectedSegmentTextColorProperty); }
1015+
set { this.SetValue(SelectedSegmentTextColorProperty, value); }
1016+
}
1017+
9961018
/// <summary>
9971019
/// Gets or sets the focused keyboard stroke for the segment.
9981020
/// </summary>
@@ -1280,6 +1302,23 @@ static void OnKeyboardFocusStrokeChanged(BindableObject bindable, object oldValu
12801302
segmentedControl._keyNavigationView?.InvalidateDrawable();
12811303
}
12821304

1305+
/// <summary>
1306+
/// Occurs when <see cref="SelectedSegmentTextColor"/> property changed.
1307+
/// </summary>
1308+
/// <param name="bindable">The bindable object.</param>
1309+
/// <param name="oldValue">The old value.</param>
1310+
/// <param name="newValue">The new value.</param>
1311+
static void OnSelectedSegmentTextColorChanged(BindableObject bindable, object oldValue, object newValue)
1312+
{
1313+
SfSegmentedControl segmentedControl = (SfSegmentedControl)bindable;
1314+
if (segmentedControl == null || segmentedControl._segmentLayout == null || !segmentedControl.IsLoaded)
1315+
{
1316+
return;
1317+
}
1318+
1319+
segmentedControl._segmentLayout?.UpdateSelectedSegmentItemStyle();
1320+
}
1321+
12831322
/// <summary>
12841323
/// Occurs when <see cref="DisabledSegmentBackground"/> property changed.
12851324
/// </summary>

maui/src/SegmentedControl/SfSegmentedControl.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public SfSegmentedControl()
8888
ThemeElement.InitializeThemeResources(this, "SfSegmentedControlTheme");
8989
SetDynamicResource(HoveredBackgroundProperty, "SfSegmentedControlHoveredBackground");
9090
SetDynamicResource(KeyboardFocusStrokeProperty, "SfSegmentedControlKeyboardFocusStroke");
91+
SetDynamicResource(SelectedSegmentTextColorProperty, "SfSegmentedControlSelectionTextColor");
9192
MinimumWidthRequest = 180;
9293
MinimumHeightRequest = 40;
9394
SelectionIndicatorSettings.Parent = this;
@@ -130,6 +131,11 @@ public SfSegmentedControl()
130131
/// </summary>
131132
Brush ISegmentInfo.KeyboardFocusStroke => KeyboardFocusStroke;
132133

134+
/// <summary>
135+
/// Gets the selected text color for the segment.
136+
/// </summary>
137+
Color ISegmentInfo.SelectedSegmentTextColor => this.SelectedSegmentTextColor;
138+
133139
#endregion
134140

135141
#region Public methods

maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Buttons/SfSegmentedControlUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ public void GetSelectedSegmentForeground_ReturnsColorFromSelectedSegmentbackgrou
12221222
var itemInfo = new SfSegmentedControl { SelectionIndicatorSettings = new SelectionIndicatorSettings { SelectionIndicatorPlacement = SelectionIndicatorPlacement.Border } };
12231223
var segmentItem = new SfSegmentItem { SelectedSegmentTextColor = Colors.Yellow };
12241224
var resultColor = SegmentViewHelper.GetSelectedSegmentForeground(itemInfo, segmentItem);
1225-
var expected = Color.FromRgba(0.40392157, 0.3137255, 0.6431373, 1);
1225+
var expected = Color.FromRgba(255, 255, 0, 255);
12261226
Assert.Equal(expected, resultColor);
12271227
}
12281228

0 commit comments

Comments
 (0)