Skip to content

Commit f78d997

Browse files
Merge pull request #1219 from telerik/new-kb-changing-bar-indicator-color-based-on-axis-size-dotnet-maui-a64db34e53874721826b797e3ad457c1
Added new kb article changing-bar-indicator-color-based-on-axis-size-dotnet-maui
2 parents ad99da5 + 67e28df commit f78d997

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Creating a Bar Indicator with Color Based on Axis Size in UI for .NET MAUI
3+
description: Learn how to create a bar indicator in UI for .NET MAUI and change its color based on axis size.
4+
type: how-to
5+
page_title: Changing Bar Indicator Color Based on Axis Size in UI for .NET MAUI
6+
meta_title: Changing Bar Indicator Color Based on Axis Size in UI for .NET MAUI
7+
slug: changing-bar-indicator-color-based-on-axis-size-dotnet-maui
8+
tags: progressbar, ui-for-dotnet-maui, axis-size, progress-fill
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.1 | ProgressBar for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to create a bar indicator and dynamically change its color based on the axis size rather than the indicator size.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to make a bar indicator color change dynamically in UI for .NET MAUI?
24+
- How to bind bar indicator color to axis size in UI for .NET MAUI?
25+
- How to use LinearProgressBar as an alternative to Gauge in UI for .NET MAUI?
26+
27+
## Solution
28+
29+
To achieve the scenario, use the [LinearProgressBar](https://docs.telerik.com/devtools/maui/controls/progressbar/overview).
30+
31+
32+
**1.** Bind the slider value to the progress bar and define a color gradient for the progress fill.
33+
34+
```xaml
35+
<VerticalStackLayout Padding="10" x:Name="vsl1">
36+
<Label Text="{Binding Value, Source={x:Reference slider}}" />
37+
<Slider Value="{Binding Data, Mode=TwoWay}" x:Name="slider" Minimum="0" Maximum="10" />
38+
39+
<VerticalStackLayout x:Name="vsl2" SizeChanged="vsl2_SizeChanged">
40+
<telerik:RadLinearProgressBar x:Name="prgsBar"
41+
Minimum="0"
42+
Maximum="10"
43+
TrackFill="Transparent"
44+
Value="{Binding Source={x:Reference slider}, Path=Value}"
45+
HeightRequest="50">
46+
<telerik:RadLinearProgressBar.ProgressFill>
47+
<LinearGradientBrush>
48+
<LinearGradientBrush.GradientStops>
49+
<GradientStop Offset="0.0" Color="Yellow" />
50+
<GradientStop Offset="0.5" Color="Red" />
51+
<GradientStop Offset="1.0" Color="Green" />
52+
</LinearGradientBrush.GradientStops>
53+
</LinearGradientBrush>
54+
</telerik:RadLinearProgressBar.ProgressFill>
55+
</telerik:RadLinearProgressBar>
56+
</VerticalStackLayout>
57+
</VerticalStackLayout>
58+
```
59+
60+
**2.** Use the `SizeChanged` event to dynamically clip the axis size.
61+
62+
```csharp
63+
public partial class MainPage : ContentPage
64+
{
65+
public MainPage()
66+
{
67+
InitializeComponent();
68+
this.BindingContext = new ViewModel();
69+
}
70+
71+
private void vsl2_SizeChanged(object sender, EventArgs e)
72+
{
73+
this.Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(100), () =>
74+
{
75+
double fromThickness = 10;
76+
PathFigure pf = new PathFigure
77+
{
78+
StartPoint = new Point(0, this.vsl2.Height)
79+
};
80+
pf.Segments.Add(new LineSegment(new Point(this.vsl2.Width, this.vsl2.Height)));
81+
pf.Segments.Add(new LineSegment(new Point(this.vsl2.Width, 0)));
82+
pf.Segments.Add(new LineSegment(new Point(0, this.vsl2.Height - fromThickness)));
83+
PathGeometry pg = new PathGeometry();
84+
pg.Figures.Add(pf);
85+
this.vsl2.Clip = pg;
86+
});
87+
}
88+
}
89+
90+
public class ViewModel : NotifyPropertyChangedBase
91+
{
92+
private double data = 10;
93+
public double Data
94+
{
95+
get => this.data;
96+
set => this.UpdateValue(ref this.data, value);
97+
}
98+
}
99+
```
100+
101+
By using `RadLinearProgressBar`, the color gradient is defined independently of the indicator size and adapts based on axis dimensions.
102+
103+
## See Also
104+
105+
- [LinearProgressBar Overview](https://docs.telerik.com/devtools/maui/controls/progressbar/overview)
106+
- [PathGeometry Class Documentation](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/shapes/geometries?view=net-maui-9.0)
107+
- [SizeChanged Event in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.visualelement.sizechanged?view=net-maui-9.0)

0 commit comments

Comments
 (0)