Hi
I've got a problem with scaling and resizing nodes which are added to Container Node. I am trying to do that while NodeModifying is happening, but always that nodes are multiplied to many times.
My code:
public DiagramNode SelectedContainer { get; set; }
private double firstContainerWidth;
private double firstContainerHeight;
public MainWindow()
{
InitializeComponent();
ShapeList.ItemsSource = Shape.Shapes.Cast<Shape>()
.Select(x => new ShapeNode
{
Shape = x,
Bounds = new Rect(0, 0, 40, 40)
});
}
private void CreateGroupButton_Click(object sender, RoutedEventArgs e)
{
if (FlowChart.Selection.Nodes.Count > 1)
{
var groupNode = SetGroupNode();
CreateGroup(groupNode, FlowChart.Selection.Nodes);
groupNode.SubordinateGroup.AutoDeleteItems = true;
FlowChart.Selection.Change(groupNode);
}
}
private void CreateGroup(ContainerNode containerNode, DiagramNodeCollection children)
{
foreach (var child in children)
{
containerNode.Add(child);
}
}
private ContainerNode SetGroupNode()
{
var containerNode = FlowChart.Factory.CreateContainerNode(0, 0, 1, 1);
containerNode.ZBottom(false);
containerNode.AutoShrink = true;
containerNode.Foldable = false;
containerNode.Caption = string.Empty;
containerNode.CaptionHeight = 0.0;
containerNode.HandlesStyle = HandlesStyle.DashFrame;
containerNode.AllowRemoveChildren = false;
containerNode.AllowAddChildren = false;
containerNode.Pen = new Pen(new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)), 0.0);
containerNode.EnabledHandles = AdjustmentHandles.All;
return containerNode;
}
private void FlowChart_NodeModifying(object sender, NodeValidationEventArgs e)
{
if (e.Node.GetType() == typeof(ContainerNode))
{
Debug.WriteLine("Selected Node:" + SelectedContainer.ActualHeight + " x " + SelectedContainer.ActualWidth);
Debug.WriteLine("Clicked:" + firstContainerHeight + " x " + firstContainerWidth);
var container = e.Node as ContainerNode;
var nodes = container.SubordinateGroup.AttachedNodes;
foreach (DiagramNode diagramNode in nodes)
{
Rect r = diagramNode.Bounds;
r.Scale(Scale(), Scale());
diagramNode.SetBounds(r, false, false);
}
}
}
private double Scale()
{
var containerAspect = SelectedContainer.ActualWidth / SelectedContainer.ActualHeight;
var childAspect = firstContainerWidth / firstContainerHeight;
double scaleFactor = 1;
if (containerAspect > childAspect)
{
scaleFactor = SelectedContainer.ActualHeight / firstContainerHeight;
}
else
{
scaleFactor = SelectedContainer.ActualWidth / firstContainerWidth;
}
return scaleFactor;
}
private void FlowChart_NodeSelected(object sender, NodeEventArgs e)
{
SelectedContainer = e.Node;
}
private void FlowChart_NodeClicked(object sender, NodeEventArgs e)
{
firstContainerHeight = e.Node.ActualHeight;
firstContainerWidth = e.Node.ActualWidth;
}
I wonder if you know how to do that, maybe even easier built in functions.
Thanks for help