Skip to main content
All docs
V24.1

How to: Bind Items Position to a Source Object

  • 2 minutes to read

This example demonstrates how to bind the Position property of diagram items to the view model in two-way mode.

Define the CustomLayoutItems event:

public Form1() {
    InitializeComponent();
    diagramDataBindingController1.CustomLayoutItems += DiagramDataBindingController1_CustomLayoutItems;
    //...
}

private void DiagramDataBindingController1_CustomLayoutItems(object sender, DiagramCustomLayoutItemsEventArgs e) {
    e.Handled = true;
}

Add a Position property of the PointFloat type to the data source:

public class Item : BindableBase {
    public int Id { get; set; }
    public string Name { get; set; }
    public PointFloat Position { get => GetProperty(() => Position); set => SetProperty(() => Position, value); }
}

Specify a DiagramBinding for the Position property:

private void DiagramDataBindingController1_GenerateItem(object sender, DiagramGenerateItemEventArgs e) {
    var item = new DiagramShape {
        X = 27,
        Width = 75,
        Height = 50,
        Shape = BasicShapes.Rectangle
    };
    item.Bindings.Add(new DiagramBinding("Position", "Position", DiagramBindingMode.TwoWay));
    e.Item = item;
}

View Example