New to Telerik UI for WinForms? Download free 30-day trial

Binding to Custom Objects

In this article you can check how to data bind RadDiagram by using custom objects.

The data source should contain columns/fields that represent the shapes/connections collections. Each of the objects that illustrates the shape/connection should have columns/fields for each of the diagram fields that you want to specify. For example, if you want to pull the width of the shape from the data source, this will require a separate column/field that contains the widths of each shape.

Consider the following classes:


public class Task : INotifyPropertyChanged
{
    private string id;
    private string text;
    private string type;
    private double x;
    private double y;
    private double width;
    private double height;

    public string Id
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
            if (this.id != value)
            {
                this.id = value;
                OnPropertyChanged("Id");
            }
        }
    }

    public string Text
    {
        get
        {
            return this.text;
        }
        set
        {
            this.text = value;
            if (this.text != value)
            {
                this.text = value;
                OnPropertyChanged("Text");
            }
        }
    }

    public string Type
    {
        get
        {
            return this.type;
        }
        set
        {
            if (this.type != value)
            {
                this.type = value;
                OnPropertyChanged("Type");
            }
        }
    }

    public double X
    {
        get
        {
            return this.x;
        }
        set
        {
            if (this.x != value)
            {
                this.x = value;
                OnPropertyChanged("X");
            }
        }
    }

    public double Y
    {
        get
        {
            return this.y;
        }
        set
        {
            this.y = value;
            if (this.y != value)
            {
                this.y = value;
                OnPropertyChanged("Y");
            }
        }
    }

    public double Height
    {
        get
        {
            return this.height;
        }
        set
        {
            if (this.height != value)
            {
                this.height = value;
                OnPropertyChanged("Height");
            }
        }
    }

    public double Width
    {
        get
        {
            return this.width;
        }
        set
        {
            if (this.width != value)
            {
                this.width = value;
                OnPropertyChanged("Width");
            }
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class Relation : INotifyPropertyChanged
{
    private string sourceTaskId;
    private string sourceConnector;
    private string targetTaskId;
    private string targetConnector;
    private string startCapField;
    private string endCapField;
    public string EndCapField
    {
        get
        {
            return this.endCapField;
        }
        set
        {
            this.endCapField = value;
            if (this.endCapField != value)
            {
                this.endCapField = value;
                OnPropertyChanged("EndCapField");
            }
        }
    }
    public string StartCapField
    {
        get
        {
            return this.startCapField;
        }
        set
        {
            this.startCapField = value;
            if (this.startCapField != value)
            {
                this.startCapField = value;
                OnPropertyChanged("StartCapField");
            }
        }
    }
    public string TargetConnector
    {
        get
        {
            return this.targetConnector;
        }
        set
        {
            this.targetConnector = value;
            if (this.targetConnector != value)
            {
                this.targetConnector = value;
                OnPropertyChanged("TargetConnector");
            }
        }
    }
    public string TargetTaskId
    {
        get
        {
            return this.targetTaskId;
        }
        set
        {
            this.targetTaskId = value;
            if (this.targetTaskId != value)
            {
                this.targetTaskId = value;
                OnPropertyChanged("TargetTaskId");
            }
        }
    }
    public string SourceConnector
    {
        get
        {
            return this.sourceConnector;
        }
        set
        {
            this.sourceConnector = value;
            if (this.sourceConnector != value)
            {
                this.sourceConnector = value;
                OnPropertyChanged("SourceConnector");
            }
        }
    }
    public string SourceTaskId
    {
        get
        {
            return this.sourceTaskId;
        }
        set
        {
            this.sourceTaskId = value;
            if (this.sourceTaskId != value)
            {
                this.sourceTaskId = value;
                OnPropertyChanged("SourceTaskId");
            }
        }
    }
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

public class Source
{
    public BindingList<Task> Tasks
    {
        get
        {
            return GetTasks();
        }
    }

    public BindingList<Relation> Relations
    {
        get
        {
            return GetRelations();
        }
    }

    private BindingList<Task> GetTasks()
    {
        return new BindingList<Task>()
        {
            new Task() { Id = "Task1", Text = "Task 1", Type = "circle", X = 100, Y = 300, Width = 50, Height = 50 },
            new Task() { Id = "Task2", Text = "Task 2", Type = "rectangle", X = 200, Y = 100, Width = 100, Height = 100 },
            new Task() { Id = "Task3", Text = "Task 3", Type = "circle", X = 300, Y = 300, Width = 50, Height = 50 },
            new Task() { Id = "Task4", Text = "Task 4", Type = "rectangle", X = 400, Y = 100, Width = 100, Height = 100 },
            new Task() { Id = "Task5", Text = "Task 5", Type = "circle", X = 500, Y = 300, Width = 50, Height = 50 }
        };
    }

    private BindingList<Relation> GetRelations()
    {
        return new BindingList<Relation>()
        {
            new Relation()
            {
                SourceTaskId = "Task2", SourceConnector = "Left", TargetTaskId = "Task1",
                TargetConnector = "Auto", StartCapField = "Arrow5Filled", EndCapField = "Arrow1"
            },
            new Relation()
            {
                SourceTaskId = "Task2", SourceConnector = "Auto", TargetTaskId = "Task3",
                TargetConnector = "Auto", StartCapField = "Arrow4Filled", EndCapField = "Arrow1Filled"
            },
            new Relation()
            {
                SourceTaskId = "Task4", SourceConnector = "Auto", TargetTaskId = "Task5",
                TargetConnector = "Auto", StartCapField = "Arrow2Filled", EndCapField = "Arrow2"
            }
        };
    }
}

The Task class will represent a single shape in RadDiagram, while the Relation class will represent a connection. Note that the Source class contains two properties, Tasks and Relations which will be specified as ConnectionDataMember and ShapeDataMember for RadDiagram. To make data binding work, you should specify the member properties as well:


this.radDiagram1.DataSource = new Source();

this.radDiagram1.ConnectionDataMember = "Relations";
this.radDiagram1.ShapeDataMember = "Tasks";
this.radDiagram1.ShapeIdMember = "Id";  
this.radDiagram1.ShapeTextMember = "Text";
this.radDiagram1.ShapeTypeMember = "Type";
this.radDiagram1.ShapeXMember = "X";
this.radDiagram1.ShapeYMember = "Y";
this.radDiagram1.ShapeWidthMember = "Width";
this.radDiagram1.ShapeHeightMember = "Height";

this.radDiagram1.ConnectionSourceShapeIdMember = "SourceTaskId";
this.radDiagram1.ConnectionTargetShapeIdMember = "TargetTaskId";
this.radDiagram1.ConnectionSourceCapTypeMember = "StartCapField";
this.radDiagram1.ConnectionTargetCapTypeMember = "EndCapField";

this.radDiagram1.ConnectionSourceConnectorMember = "SourceConnector";
this.radDiagram1.ConnectionTargetConnectorMember = "TargetConnector";

WinForms RadDiagram DataBinding Custom Objects

In this article