I am using GetTemplateChild as follow, but it always returns NULL. How to fix this?
[TemplatePart(Name = "textPoints", Type = typeof(TextBlock))]
textPoints = (TextBlock)GetTemplateChild("TextBlock");
GetTemplateChild takes the name as a parameter, not the type. Since your XAML is defined as:
<TextBlock Text="{Binding}" Foreground="Cyan"
x:Name="textPoints"
Try passing "textPoints"
instead of "TextBlock"
as the name to retrieve:
[TemplatePart(Name = "textPoints", Type = typeof(TextBlock))]
textPoints = (TextBlock)GetTemplateChild("textPoints");
Looks like you are trying to get template child of some other control, from where you are calling GetTemplateChild?
If your ItemsControl is inside some UserControl then GetTemplateChild will not work as children of your UserControl are not part of template child anyway and it will not recursively search every child's template child.
Mostly GetTemplateChild is used in Custom Controls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With