Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Custom positioning of elements (Read 3365 times)
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Custom positioning of elements
May 26th, 2011 at 12:28pm
Print Post  
Hello,

I am currently evaluating MindFusion WPF Diagram for our company, and I have a question.

Our application is using MVVM pattern for the User interface. Updates are coming from the model, and a list of elements which should be visible would appear in an observable collection in the view model. I need to create a vew, which would use databinding to this collection, and would convert such collection into diagram nodes on every update (meaning, add new nodes, remove old ones, and position them all). Does anyone have a suggestion, which would be the most optimal way to do this?

Cheers
Petr
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Custom positioning of elements
Reply #1 - May 26th, 2011 at 1:32pm
Print Post  
Hi,

You could use this class in your view, and bind its DataItems property to the collection:

Code
Select All
using System.Collections;
using System.Collections.Specialized;
using System.Windows;

using MindFusion.Diagramming.Wpf;

class BoundDiagram : Diagram
{
	public static DependencyProperty DataItemsProperty = DependencyProperty.Register(
		"DataItems",
		typeof(IList),
		typeof(BoundDiagram),
		new PropertyMetadata(null, OnDataItemsChanged));

	public IList DataItems
	{
		get { return (IList)GetValue(DataItemsProperty); }
		set { SetValue(DataItemsProperty, value); }
	}

	private static void OnDataItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		var diagram = (BoundDiagram)d;
		diagram.OnDataItemsChanged(
			(INotifyCollectionChanged)e.OldValue,
			(INotifyCollectionChanged)e.NewValue);
	}

	private void OnDataItemsChanged(
		INotifyCollectionChanged oldItems,
		INotifyCollectionChanged newItems)
	{
		if (oldItems != null)
		{
			oldItems.CollectionChanged -= DataItemsCollectionChanged;
			ClearAll();
		}
		if (newItems != null)
		{
			newItems.CollectionChanged += DataItemsCollectionChanged;
			AddItems(DataItems);
		}
	}

	void DataItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		if (e.Action == NotifyCollectionChangedAction.Reset)
			ClearAll();
		else if (e.Action == NotifyCollectionChangedAction.Add)
			AddItems(e.NewItems);
		else if (e.Action == NotifyCollectionChangedAction.Remove)
			RemoveItems(e.OldItems);
	}

	void AddItems(IList items)
	{
		foreach (var item in items)
		{
			var node = Factory.CreateShapeNode(0, 0, 40, 20);
			node.Text = item.ToString();
			node.Tag = item;
		}
		Arrange();
	}

	void RemoveItems(IList items)
	{
		foreach (var item in items)
			Nodes.Remove(FindNode(item));
		Arrange();
	}

	void Arrange()
	{
		double y = 20;
		foreach (DiagramNode node in Nodes)
		{
			node.Move(20, y);
			y += 35;
		}
		// todo: run some layout algorithm if you also create links
	}
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Custom positioning of elements
Reply #2 - May 26th, 2011 at 1:52pm
Print Post  
That seems to be exactly what I need! Huge thanks!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint