Articles in this section
Category / Section

How to bind range collection to scale in Xamarin.Forms SfCircularGauge

1 min read

Circular gauge supports multiple ranges, and you can bind the ranges collection. This KB article explains the steps required to bind a property to the Ranges collection of SfCircularGauge.

 

Step 1: Create a ViewModel class and declare a property of type ObservableCollection<Range>. Define each range with the required properties and add it to the Ranges collection.

 

Refer to the following code sample for defining the collection property in view model.

 

C#:

 

     
        public ObservableCollection<Range> Ranges
        {
            get;
            set;
        }
 
        public ViewModel()
        {
            Ranges = new ObservableCollection<Range>();
         
            Range range = new Range();
            range.StartValue = 0;
            range.EndValue = 35;
            range.Offset = 0.7;
            range.Thickness = 30;
            range.Color = Color.FromHex("#F03E3E");
 
            Range range1 = new Range();
            range1.StartValue = 35;
            range1.EndValue = 75;
            range1.Offset = 0.7;
            range1.Thickness = 30;
            range1.Color = Color.FromHex("#FFDD00");
            
            Range range2 = new Range();
            range2.StartValue = 75;
            range2.EndValue = 100;
            range2.Offset = 0.7;
            range2.Thickness = 30;
            range2.Color = Color.FromHex("#27beb7");
 
            Ranges.Add(range);
            Ranges.Add(range1);
            Ranges.Add(range2);
        }
    }

 

Step 2: Set the BindingContext for gauge or the parent of gauge (e.g., ContentPage) as ViewModel. So, binding will work by taking that context.

 

Refer to the following code sample for setting binding context.

 

XAML:

 

<ContentPage.BindingContext>
        <local:ViewModel/>
</ContentPage.BindingContext>
 

 

Step 3: Bind the collection property defined in the ViewModel to the Ranges property of circular gauge’s scale as demonstrated in the following code sample.

 

XAML:

 

<gauge:SfCircularGauge>
 
     <gauge:SfCircularGauge.Scales>
 
            <gauge:Scale Ranges="{Binding Ranges}"/>
            
      </gauge:SfCircularGauge.Scales>
 
</gauge:SfCircularGauge>

 

Output

Xamarin.Forms SfCircularGauge Ranges

 

 

You can download the sample from the below link: Sample

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied