windowsforms-devexpress-dot-xtracharts-dot-designer-dot-chartdesigner-dot-registercustommodeltype-x28-system-dot-type-system-dot-type-x29.md
Associates a model type with a chart element type.
Namespace : DevExpress.XtraCharts.Designer
Assembly : DevExpress.XtraCharts.v25.2.Wizard.dll
NuGet Package : DevExpress.Win.Charts
public void RegisterCustomModelType(
Type chartElementType,
Type customModelType
)
Public Sub RegisterCustomModelType(
chartElementType As Type,
customModelType As Type
)
| Name | Type | Description |
|---|---|---|
| chartElementType | Type |
The chart element type.
| | customModelType | Type |
The model type.
|
Use the RegisterCustomModelType method to register a model for a custom chart element.
This example illustrates how to create and register a model (the CustomPointColorizerModel class in this example) for a custom colorizer (the CustomPointColorizer class in this example).
View Example: How to Create a Model for a Custom Chart Element
Step 1. Create a Model
For example, you have a custom colorizer CustomPointColorizer class that looks as follows:
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomPointColorizer : ChartColorizerBase {
double threshold = 60;
Color lower = Color.Red;
Color upper = Color.Green;
public double Value {
get { return threshold; }
set { threshold = value; }
}
public Color LowerValuePointColor {
get { return lower; }
set { lower = value; }
}
public Color UpperValuePointColor {
get { return upper; }
set { upper = value; }
}
public override Color GetAggregatedPointColor(object argument, object[] values, SeriesPoint[] points, Palette palette) {
if ((double)values[0] > Value)
return UpperValuePointColor;
else
return LowerValuePointColor;
}
public override Color GetPointColor(object argument, object[] values, object colorKey, Palette palette) {
return Color.Empty;
}
protected override ChartElement CreateObjectForClone() {
return new CustomPointColorizer();
}
public override void Assign(ChartElement obj) {
base.Assign(obj);
CustomPointColorizer colorizer = obj as CustomPointColorizer;
if (colorizer != null) {
Value = colorizer.Value;
LowerValuePointColor = colorizer.LowerValuePointColor;
UpperValuePointColor = colorizer.UpperValuePointColor;
}
}
public override string ToString() {
return "(CustomPointColorizer)";
}
}
<TypeConverter(GetType(ExpandableObjectConverter))>
Public Class CustomPointColorizer
Inherits ChartColorizerBase
Private threshold As Double = 60
Private lower As Color = Color.Red
Private upper As Color = Color.Green
Public Property Value As Double
Get
Return threshold
End Get
Set(ByVal value As Double)
threshold = value
End Set
End Property
Public Property LowerValuePointColor As Color
Get
Return lower
End Get
Set(ByVal value As Color)
lower = value
End Set
End Property
Public Property UpperValuePointColor As Color
Get
Return upper
End Get
Set(ByVal value As Color)
upper = value
End Set
End Property
Public Overrides Function GetAggregatedPointColor(ByVal argument As Object, ByVal values As Object(), ByVal points As SeriesPoint(), ByVal palette As Palette) As Color
If CDbl(values(0)) > Value Then
Return UpperValuePointColor
Else
Return LowerValuePointColor
End If
End Function
Public Overrides Function GetPointColor(ByVal argument As Object, ByVal values As Object(), ByVal colorKey As Object, ByVal palette As Palette) As Color
Return Color.Empty
End Function
Protected Overrides Function CreateObjectForClone() As ChartElement
Return New CustomPointColorizer
End Function
Public Overrides Sub Assign(ByVal obj As ChartElement)
MyBase.Assign(obj)
Dim colorizer = TryCast(obj, CustomPointColorizer)
If colorizer IsNot Nothing Then
Value = colorizer.Value
LowerValuePointColor = colorizer.LowerValuePointColor
UpperValuePointColor = colorizer.UpperValuePointColor
End If
End Sub
Public Overrides Function ToString() As String
Return "(CustomPointColorizer)"
End Function
End Class
Next, create a CustomPointColorizerModel model for your colorizer. This model is used to modify the colorizer options in the Chart Designer’s Properties tab.
public class CustomPointColorizerModel : ChartColorizerBaseModel {
CustomPointColorizer MyColorizer { get { return (CustomPointColorizer)Colorizer; } }
public double Value {
get { return MyColorizer.Value; }
set { SetProperty("Value", value); }
}
public Color LowerValuePointColor {
get { return MyColorizer.LowerValuePointColor; }
set { SetProperty("LowerValuePointColor", value); }
}
public Color UpperValuePointColor {
get { return MyColorizer.UpperValuePointColor; }
set { SetProperty("UpperValuePointColor", value); }
}
public CustomPointColorizerModel(ChartColorizerBase element, CustomModelProvider customModelProvider) : base(element, customModelProvider) {
}
}
Public Class CustomPointColorizerModel
Inherits ChartColorizerBaseModel
Private ReadOnly Property MyColorizer As CustomPointColorizer
Get
Return CType(Colorizer, CustomPointColorizer)
End Get
End Property
Public Property Value As Double
Get
Return MyColorizer.Value
End Get
Set(ByVal value As Double)
SetProperty("Value", value)
End Set
End Property
Public Property LowerValuePointColor As Color
Get
Return MyColorizer.LowerValuePointColor
End Get
Set(ByVal value As Color)
SetProperty("LowerValuePointColor", value)
End Set
End Property
Public Property UpperValuePointColor As Color
Get
Return MyColorizer.UpperValuePointColor
End Get
Set(ByVal value As Color)
SetProperty("UpperValuePointColor", value)
End Set
End Property
Public Sub New(ByVal element As ChartColorizerBase, ByVal customModelProvider As CustomModelProvider)
MyBase.New(element, customModelProvider)
End Sub
End Class
Step 2. Customize the Property Editor
Use the Editor attribute to configure an editor for a Chart Designer property. In this example, you add the CustomPointColorizer item to the list of colorizers for the bar series model’s Colorizer property.
public class SideBySideBarSeriesViewCustomModel : SideBySideBarSeriesViewModel {
[Editor(typeof(CustomColorizerEditor), typeof(UITypeEditor))]
public new ChartColorizerBaseModel Colorizer {
get { return base.Colorizer; }
set { base.Colorizer = value; }
}
public SideBySideBarSeriesViewCustomModel(SideBySideBarSeriesView element, CustomModelProvider customModelProvider)
: base(element, customModelProvider) {
}
}
public class CustomColorizerEditor : UITypeEditor {
IWindowsFormsEditorService editorService;
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
var seriesView = context.Instance as SeriesViewBaseModel;
List<ChartColorizerBase> colorizers = GetColorizers();
CustomModelProvider modelProvider = new CustomModelProvider();
modelProvider.RegisterCustomModelType(typeof(CustomPointColorizer), typeof(CustomPointColorizerModel));
var listBox = new ListBoxControl();
listBox.Click += listBox_Click;
listBox.Items.Add("(None)");
foreach (ChartColorizerBase colorizer in colorizers) {
var colorizerModel = ModelHelper.GetModel<ChartColorizerBaseModel>(colorizer, modelProvider);
int index = listBox.Items.Add(colorizerModel);
if (value != null && colorizerModel.GetType() == value.GetType()) {
listBox.SelectedIndex = index;
}
}
editorService.DropDownControl(listBox);
if (listBox.SelectedIndex != 0)
if (value == null || listBox.SelectedItem.GetType() != value.GetType())
return listBox.SelectedItem;
else
return value;
else
return null;
}
void listBox_Click(object sender, EventArgs e) {
this.editorService.CloseDropDown();
}
List<ChartColorizerBase> GetColorizers() {
return new List<ChartColorizerBase>() { new CustomPointColorizer(), new KeyColorColorizer(), new RangeColorizer() };
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.DropDown;
}
}
Public Class SideBySideBarSeriesViewCustomModel
Inherits SideBySideBarSeriesViewModel
<Editor(GetType(CustomColorizerEditor), GetType(UITypeEditor))>
Public Overloads Property Colorizer As ChartColorizerBaseModel
Get
Return MyBase.Colorizer
End Get
Set(ByVal value As ChartColorizerBaseModel)
MyBase.Colorizer = value
End Set
End Property
Public Sub New(ByVal element As SideBySideBarSeriesView, ByVal customModelProvider As CustomModelProvider)
MyBase.New(element, customModelProvider)
End Sub
End Class
Public Class CustomColorizerEditor
Inherits UITypeEditor
Private editorService As IWindowsFormsEditorService
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
editorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
Dim seriesView = TryCast(context.Instance, SeriesViewBaseModel)
Dim colorizers = GetColorizers
Dim modelProvider As CustomModelProvider = New CustomModelProvider
modelProvider.RegisterCustomModelType(GetType(CustomPointColorizer), GetType(CustomPointColorizerModel))
Dim listBox = New ListBoxControl
listBox.Click += AddressOf listBox_Click
listBox.Items.Add("(None)")
For Each colorizer As ChartColorizerBase In colorizers
Dim colorizerModel = ModelHelper.GetModel(Of ChartColorizerBaseModel)(colorizer, modelProvider)
Dim index As Integer = listBox.Items.Add(colorizerModel)
If value IsNot Nothing AndAlso colorizerModel.GetType Is value.GetType Then
listBox.SelectedIndex = index
End If
Next
editorService.DropDownControl(listBox)
If listBox.SelectedIndex IsNot 0 Then
If value Is Nothing OrElse listBox.SelectedItem.GetType IsNot value.GetType Then
Return listBox.SelectedItem
Else
Return value
End If
Else
Return Nothing
End If
End Function
Private Sub listBox_Click(ByVal sender As Object, ByVal e As EventArgs)
editorService.CloseDropDown
End Sub
Private Function GetColorizers() As List(Of ChartColorizerBase)
Return New List(Of ChartColorizerBase) From {
New CustomPointColorizer,
New KeyColorColorizer,
New RangeColorizer
}
End Function
Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.DropDown
End Function
End Class
Step 3. Register Models
To associate the model with the chart element, use the ChartDesigner.RegisterCustomModelType(System.Type,System.Type) method.
void OnButtonClick(object sender, EventArgs e) {
ChartDesigner chartDesigner = new ChartDesigner((ChartControl)this.Controls["MyChart"]);
chartDesigner.RegisterCustomModelType(typeof(CustomPointColorizer), typeof(CustomPointColorizerModel));
chartDesigner.RegisterCustomModelType(typeof(SideBySideBarSeriesView), typeof(SideBySideBarSeriesViewCustomModel));
chartDesigner.ShowDialog(true);
}
Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim chartDesigner As ChartDesigner = New ChartDesigner(CType(Me.Controls("MyChart"), ChartControl))
chartDesigner.RegisterCustomModelType(GetType(CustomPointColorizer), GetType(CustomPointColorizerModel))
chartDesigner.RegisterCustomModelType(GetType(SideBySideBarSeriesView), GetType(SideBySideBarSeriesViewCustomModel))
chartDesigner.ShowDialog(True)
End Sub
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the RegisterCustomModelType(Type, Type) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-charts-create-model-for-custom-chart-element/CS/CustomChartElementModel/Form1.cs#L14
ChartDesigner chartDesigner = new ChartDesigner((ChartControl)this.Controls["MyChart"]);
chartDesigner.RegisterCustomModelType(typeof(CustomPointColorizer), typeof(CustomPointColorizerModel));
chartDesigner.RegisterCustomModelType(typeof(SideBySideBarSeriesView), typeof(SideBySideBarSeriesViewCustomModel));
winforms-charts-create-model-for-custom-chart-element/VB/CustomChartElementModel/Form1.vb#L17
Dim chartDesigner As ChartDesigner = New ChartDesigner(CType(Me.Controls("MyChart"), ChartControl))
chartDesigner.RegisterCustomModelType(GetType(CustomPointColorizer), GetType(CustomPointColorizerModel))
chartDesigner.RegisterCustomModelType(GetType(SideBySideBarSeriesView), GetType(SideBySideBarSeriesViewCustomModel))
See Also