Data binding doesn't work when the Gauge control is hosted inside a Custom Control (we need to use Custom control for better performance)
See sample code below, the Gauge in MainWindow binds fine with the Ranges, but not the Gauge inside the custom control.
MainWindow.xaml
MainWindow.cs
RadialGaugeControl.cs
Theme\Generic.xaml
App.xaml
See sample code below, the Gauge in MainWindow binds fine with the Ranges, but not the Gauge inside the custom control.
MainWindow.xaml
<
Window
x:Class
=
"GaugeTest.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local
=
"clr-namespace:GaugeTest"
Title
=
"MainWindow"
Height
=
"638"
Width
=
"575"
>
<
StackPanel
>
<
telerik:RadRadialGauge
Name
=
"PART_Gauge"
Width
=
"220"
Height
=
"220"
>
<
telerik:RadialScale
Name
=
"scale"
RangeLocation
=
"Outside"
LabelRotationMode
=
"None"
Radius
=
"0.93"
Ranges
=
"{Binding Ranges}"
>
<
telerik:RadialScale.Indicators
>
<
telerik:BarIndicator
Name
=
"radialBar"
UseRangeColor
=
"True"
RangeColorMode
=
"ProportionalBrush"
StartWidth
=
"0.06"
telerik:ScaleObject.Location
=
"Outside"
telerik:ScaleObject.Offset
=
"0.01*"
Value
=
"0"
/>
</
telerik:RadialScale.Indicators
>
</
telerik:RadialScale
>
</
telerik:RadRadialGauge
>
<
local:RadialGaugeControl
Width
=
"220"
Height
=
"220"
/>
</
StackPanel
>
</
Window
>
MainWindow.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Gauge;
namespace GaugeTest
{
/// <
summary
>
/// Interaction logic for MainWindow.xaml
/// </
summary
>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
scale.SetCurrentValue(ScaleBase.RangesProperty, new GaugeRangeCollection());
this.DataContext = new MainWindowViewModel();
}
}
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
Ranges = new GaugeRangeCollection();
AddRange(0, 30, Colors.Green);
AddRange(30, 60, Colors.Yellow);
AddRange(60, 100, Colors.Orange);
}
private void AddRange(double min, double max, Color color)
{
GaugeRange gr = new GaugeRange()
{
Min = min,
Max = max,
StartWidth = 0.01,
EndWidth = 0.01,
Background = new SolidColorBrush(color),
IndicatorBackground = new SolidColorBrush(color),
TickBackground = new SolidColorBrush(color),
LabelForeground = new SolidColorBrush(color),
};
gr.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "Background")
{
// make sure they are in sync
gr.IndicatorBackground = gr.Background;
gr.TickBackground = gr.Background;
gr.LabelForeground = gr.Background;
}
};
Ranges.Add(gr);
}
private GaugeRangeCollection _ranges;
public GaugeRangeCollection Ranges
{
get
{
return _ranges;
}
set
{
_ranges = value;
OnPropertyChanged(() => Ranges);
}
}
}
}
RadialGaugeControl.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace GaugeTest
{
public class RadialGaugeControl : Control
{
static RadialGaugeControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RadialGaugeControl), new FrameworkPropertyMetadata(typeof(RadialGaugeControl)));
}
}
}
Theme\Generic.xaml
<
ResourceDictionary
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local
=
"clr-namespace:GaugeTest"
>
<
Style
TargetType
=
"{x:Type local:RadialGaugeControl}"
>
<
Setter
Property
=
"Template"
>
<
Setter.Value
>
<
ControlTemplate
TargetType
=
"{x:Type local:RadialGaugeControl}"
>
<
Border
Background
=
"{TemplateBinding Background}"
BorderBrush
=
"{TemplateBinding BorderBrush}"
BorderThickness
=
"{TemplateBinding BorderThickness}"
>
<
telerik:RadRadialGauge
Name
=
"PART_Gauge"
Width
=
"220"
Height
=
"220"
>
<
telerik:RadialScale
Name
=
"PART_Scale"
RangeLocation
=
"Outside"
LabelRotationMode
=
"None"
Radius
=
"0.93"
Ranges
=
"{Binding Ranges}"
>
<
telerik:RadialScale.Indicators
>
<
telerik:BarIndicator
Name
=
"radialBar"
UseRangeColor
=
"True"
RangeColorMode
=
"ProportionalBrush"
StartWidth
=
"0.06"
telerik:ScaleObject.Location
=
"Outside"
telerik:ScaleObject.Offset
=
"0.01*"
Value
=
"0"
/>
</
telerik:RadialScale.Indicators
>
</
telerik:RadialScale
>
</
telerik:RadRadialGauge
>
</
Border
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
</
ResourceDictionary
>
App.xaml
<
Application
x:Class
=
"GaugeTest.App"
StartupUri
=
"MainWindow.xaml"
>
<
Application.Resources
>
<
ResourceDictionary
>
<
ResourceDictionary.MergedDictionaries
>
<
ResourceDictionary
Source
=
"/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml"
/>
<
ResourceDictionary
Source
=
"/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.DataVisualization.xaml"
/>
</
ResourceDictionary.MergedDictionaries
>
</
ResourceDictionary
>
</
Application.Resources
>
</
Application
>