Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Styling of ContainerNodes (Read 2525 times)
benjamin huser
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 26
Joined: Oct 23rd, 2014
Styling of ContainerNodes
Oct 23rd, 2014 at 11:43am
Print Post  
Hi

we are currently working with the ContainerNode. Rightnow there are only some properties to define the style of the node (Background, Border etc.).
We would like to have full control over the style and, if possible, do this in XAML and not in the Draw-Method of the ContainerNode. Is this possible, and if yes, how?

Furthermore we would like to hide the disabled MovingHandles. Is this somehow possible?

Thanks for your help
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Styling of ContainerNodes
Reply #1 - Oct 23rd, 2014 at 4:11pm
Print Post  
Hi,

If you mean specifying containers' appearance via Xaml template, you will have to add a Template property and return its instance from GetVisualChild override in a derived class, while also overriding Draw to prevent standard rendering:

Code
Select All
<Style TargetType="{x:Type local:MyContainerNode}">
	<Setter Property="Template">
		<Setter.Value>
			<DataTemplate DataType="{x:Type local:MyContainerNode}">
				<Border
					BorderBrush="{Binding Stroke}"
					BorderThickness="1"
					Background="{Binding Brush}">
					<Grid>
						<Grid.RowDefinitions>
							<RowDefinition Height="Auto" />
							<RowDefinition Height="*" />
						</Grid.RowDefinitions>
						<TextBlock Height="{Binding CaptionHeight}" Text="{Binding Text}" />
					</Grid>
				</Border>
			</DataTemplate>
		</Setter.Value>
	</Setter>
</Style>

class MyContainerNode : ContainerNode
{
	static MyContainerNode()
	{
		DefaultStyleKeyProperty.OverrideMetadata(
			typeof(MyContainerNode),
			new FrameworkPropertyMetadata(typeof(MyContainerNode)));
	}
	/// <summary>
	/// Initializes a new instance of the MyContainerNode class.
	/// </summary>
	public MyContainerNode(Diagram parent)
		: base(parent)
	{
		EnsureTemplate();
	}

	public override void Draw(DrawingContext graphics,
		MindFusion.Diagramming.Wpf.RenderOptions options)
	{
		// Draw the fold button
		DrawManipulators(graphics, false);
	}

	protected override void OnAdd()
	{
		base.OnAdd();
		EnsureTemplate();
	}

	internal void EnsureTemplate()
	{
		if (visual == null)
			OnTemplateChanged();
	}

	public static DependencyProperty TemplateProperty = DependencyProperty.Register(
		"Template", typeof(DataTemplate), typeof(MyContainerNode),
		new FrameworkPropertyMetadata(null, OnTemplateChanged));

	static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
	{
		var node = (MyContainerNode)d;
		node.OnTemplateChanged();
	}

	void OnTemplateChanged()
	{
		var template = Template;
		if (template == null)
			return;

		if (visual != null)
			RemoveVisualChild(visual);

		visual = template.LoadContent() as FrameworkElement;
		if (visual != null)
		{
			visual.DataContext = this;
			AddVisualChild(visual);
			InvalidateVisual();
		}
	}

	public DataTemplate Template
	{
		get { return (DataTemplate)GetValue(TemplateProperty); }
		set { SetValue(TemplateProperty, value); }
	}

	protected override Size MeasureOverride(Size availableSize)
	{
		if (visual != null)
			visual.Measure(Bounds.Size);

		return Bounds.Size;
	}

	protected override Size ArrangeOverride(Size finalSize)
	{
		if (visual != null)
			visual.Arrange(new Rect(Bounds.Size));
		return Bounds.Size;
	}

	protected override int VisualChildrenCount
	{
		get { return visual == null ? 0 : 1; }
	}

	protected override Visual GetVisualChild(int index)
	{
		return visual;
	}

	private FrameworkElement visual;
}
 



Set Diagram.ShowDisabledHandles = false to hide disable handles.

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


I Love MindFusion!

Posts: 26
Joined: Oct 23rd, 2014
Re: Styling of ContainerNodes
Reply #2 - Oct 24th, 2014 at 8:42am
Print Post  
Hi Stoyan,

yes that was exactly what I wanted to achieve.
I implemented your solution and it is working perfect.

Thanks for your quick answer.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Styling of ContainerNodes
Reply #3 - Oct 29th, 2014 at 2:07pm
Print Post  
This version adds Template property to the base DiagramNode class:
https://mindfusion.eu/_beta/wpfdiag.3.2.zip

So now you can specify appearance through Xaml for all built-in node types by just adding Style with TargetType to the resource dictionary:

Code
Select All
<ResourceDictionary>
	<Style TargetType="{x:Type diag:ContainerNode}">
		<Setter Property="Template">
			<Setter.Value>
				<DataTemplate DataType="{x:Type diag:ContainerNode}">.... 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Styling of ContainerNodes
Reply #4 - Oct 31st, 2014 at 10:43am
Print Post  
Here we have also added a NodeRenderer class that implements standard visualization of nodes, so using it you can now add extra Xaml elements on top of nodes without redefining all graphics:
https://mindfusion.eu/_beta/wpfdiag.3.2.zip

Code
Select All
<Style TargetType="{x:Type diag:ContainerNode}">
	<Setter Property="Template">
		<Setter.Value>
			<DataTemplate DataType="{x:Type diag:ContainerNode}">
				<Grid>
					<diag:NodeRenderer Node="{Binding}" />
					<Line X2="100" Y2="100" Stroke="Red" />
				</Grid>
			</DataTemplate>
		</Setter.Value>
	</Setter>
</Style> 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint