I used a xaml file to store node's datamtemplate, as follows:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converter="clr-namespace:TemplateView.converters"
xmlns:model="clr-namespace:TemplateView.Model" >
<DataTemplate x:Key="nodedt">
<Border BorderThickness="1" Background ="#2E95D8"
BorderBrush="LightGray">
<Grid>
<TextBlock Text="{Binding Name}" />
</Grid>
</Border>
</DataTemplate>
</ResourceDictionary>
Here I want bind text with node's property "Name"
And window xaml file:
<Sync:SfDiagram x:Name="diagram" Nodes="{Binding Nodes}" >
<Sync:SfDiagram.Connectors>
<!--Observable Collection of ConnectorViewModel-->
<Sync:ConnectorCollection/>
</Sync:SfDiagram.Connectors>
</Sync:SfDiagram>
In the viewmodel cs file:
TemplateCustomNode node = new TemplateCustomNode()
{
Name="example text",
UnitHeight = 100,
UnitWidth = 100,
OffsetX = offsetx,
OffsetY = offsety,
ContentTemplate = resourceDictionary["nodedt"] as DataTemplate
};
Nodes.Add(node);
Now I can see the node in the diagram with data template border,
but I can't see the binding Name text in the textblock.
I don't know why binding is out of work.
console show:
BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=TextDecorations; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'TextDecorations' (type 'TextDecorationCollection')
I have no idea to solve the problem.