We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

data template binding error

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.










1 Reply

KR Karkuvel Rajan Shanmugavel Syncfusion Team April 26, 2023 02:10 PM UTC

Hi Zhang Yabin,


We have analyzed the reported issue of “ Binding error occurs in DataTemplate” with the provided codes. In the provided codes we have found that you have set TemplateBinding with the Name property and assign that template as ContentTemplate of Node. From this we understood that you want to display the name of the Node in your ContentTemplate. In Default , Content is act as the DataContext for the ContentTemplate. So, if you want to bind any property in the ContentTemplate then you have that property in your Content class. We have prepared a simple sample to achieve your requirement. Please follow the sample in below attachment.


Regards,

Karkuvel Rajan S


Attachment: Simple_Sample_88379505.zip

Loader.
Live Chat Icon For mobile
Up arrow icon