Get reference of UIElement inside NodeTemplate

Hello,

I want to get reference of UI object inside Node Template using node key.

Please check event btnChangeColor_Click where I am getting reference of node which is of type MyNode. How to get reference of txtNodeText using the node?

public partial class test : UserControl<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

public test()

{

InitializeComponent();

try

{

var model = new GraphLinksModel<MyNode, String, String, MyLink>();

model.Modifiable = true;

myDiagram.StartTransaction("AddNode");

MyNode mainNode = new MyNode("A", "1");

model.AddNode(mainNode);

MyNode nodeB = new MyNode("B", "2");

model.AddNode(nodeB);

model.AddLink(mainNode, null, nodeB, null);

myDiagram.CommitTransaction("AddNode");

myDiagram.Model = model;

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void CollapseExpandButton_Click(object sender, RoutedEventArgs e)

{

}

private void btnChangeColor_Click(object sender, RoutedEventArgs e)

{

MyNode node = myDiagram.Model.FindNodeByKey("1") as MyNode;

if (node != null)

{

//Here I want to get reference of txtNodeText object which is inside node template

}

}

}

public class MyNode : GraphLinksModelNodeData<String>

{

public MyNode()

{

this.MyNodeText = "";

this.ShowButton = Visibility.Visible;

}

public MyNode(String text, String key)

{

this.Key = key;

this.MyNodeText = text;

this.ShowButton = Visibility.Visible;

}

public String MyNodeText { get; set; }

public Visibility ShowButton { get; set; }

}

public class MyLink : GraphLinksModelLinkData<String, String>

{

public String LabelText

{

get

{

return "Is-A";

}

}

}

<UserControl x:Class="TermGraphViewer.test"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:go="clr-namespace:Northwoods.GoXam;assembly=Northwoods.GoSilverlight"

xmlns:golayout="clr-namespace:Northwoods.GoXam.Layout;assembly=Northwoods.GoSilverlight"

Height="700">

<UserControl.Resources>

<DataTemplate x:Key="NodeTemplate">

<StackPanel Orientation="Vertical" go:Part.SelectionAdorned="True"

go:Node.IsTreeExpanded="True">

<Button Visibility="{Binding Path=Data.ShowButton}" x:Name="myCollapseExpandButton"

Click="CollapseExpandButton_Click" FontSize="8" VerticalAlignment="Top"

Content="+" Width="15" Height="15" />

<go:NodePanel Sizing="Auto">

<Path go:NodePanel.Figure="Rectangle" Fill="LightGray" />

<TextBlock x:Name="txtNodeText" Text="{Binding Path=Data.MyNodeText}" MinWidth="100" MaxWidth="100"

TextWrapping="Wrap" ToolTipService.ToolTip="{Binding Path=Data.MyNodeText }" />

</go:NodePanel>

</StackPanel>

</DataTemplate>

<DataTemplate x:Key="LinkTemplate">

<go:LinkPanel go:Part.SelectionElementName="Path" go:Part.SelectionAdorned="False">

<go:Link.Route>

<go:Route RelinkableFrom="False" RelinkableTo="False" />

</go:Link.Route>

<Path go:LinkPanel.IsLinkShape="True" x:Name="Path" Stroke="Black" StrokeThickness="1" />

<Polyline Stroke="Black" StrokeThickness="1"

Points="0 0 8 4 0 8" go:LinkPanel.Alignment="1 0.5"

go:LinkPanel.Index="-1" go:LinkPanel.Orientation="Along" />

<TextBlock Text="{Binding Path=Data.LabelText, Mode=TwoWay}" go:LinkPanel.Offset="8 8"

FontSize="9" go:Part.TextEditable="False" go:LinkPanel.Orientation="Upright" />

</go:LinkPanel>

</DataTemplate>

</UserControl.Resources>

<Grid x:Name="LayoutRoot" VerticalAlignment="Stretch">

<StackPanel Orientation="Vertical" >

<StackPanel Orientation="Horizontal">

<Button x:Name="btnChangeColor" Content="Change Color" Click="btnChangeColor_Click"></Button>

</StackPanel>

<StackPanel VerticalAlignment="Stretch" Height="600" >

<go:Diagram Grid.Row="1" x:Name="myDiagram" Height="600"

Padding="10"

NodeTemplate="{StaticResource NodeTemplate}"

LinkTemplate="{StaticResource LinkTemplate}">

<go:Diagram.Layout>

<golayout:LayeredDigraphLayout LayerSpacing="150" />

</go:Diagram.Layout>

</go:Diagram>

</StackPanel>

</StackPanel>

</Grid>

</UserControl>

Thanks

Hardik

MyNode data = myDiagram.Model.FindNodeByKey("1") as MyNode; Node node = myDiagram.PartManager.FindNodeForData(data); if (node != null) { TextBlock tb = node.FindNamedDescendant("txtNodeText") as TextBlock; if (tb != null) { . . . } }
By the way, naming your data class “MyNode” can be a bit confusing, since it seems it should be a Node FrameworkElement rather than your application data object.

It worked. Thanks for quick reply.