This is a migrated thread and some comments may be shown as answers.

Snap to grid when dropping new item

1 Answer 169 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Gijs
Top achievements
Rank 1
Gijs asked on 02 Feb 2015, 04:44 PM
Hi,

I have a toolbox with custom shapes. I have a grid with snapping to grid enabled.
Once shapes are on the grid, they snap fine when I drag them.
But they don't snap the first time, when I drag shapes from the toolbox.

I would like to achieve this. Is there some setting I'm missing? Or would I have to implement a custom Snapping Service maybe?

Thanks for any input.

1 Answer, 1 is accepted

Sort by
0
Zarko
Telerik team
answered on 04 Feb 2015, 02:25 PM
Hi Gijs,
By default we don't snap the drop position but you can change this using the ItemsChanged event:
private void OnDiagramItemsChanged(object sender, DiagramItemsChangedEventArgs e)
{
    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
    {
        var shapService = this.diagram.ServiceLocator.GetService<ISnappingService>();
        foreach (var item in e.NewItems)
        {
            var container = this.diagram.ContainerGenerator.ContainerFromItem(item) as IShape;
            if (container != null)
            {
                var newPosition = shapService.SnapPoint(container.Position);
                container.Position = newPosition;
            }
        }
    }
}
I hope I was able to help you and if you have more questions feel free to ask.

Regards,
Zarko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Diagram
Asked by
Gijs
Top achievements
Rank 1
Answers by
Zarko
Telerik team
Share this question
or