Saturday, October 10, 2015

Access TemplatedElements in CustomControl

How to access TemplatedElements in CustomControl?
Override OnApplyTemplate in the .cs file. Its called everytime a template is applied to a control

public override void OnApplyTenplate()
{
base.OnApplyTemplate();

var textblock= GetTemplateChild("Name Of Part") as TextBlock;

textblock.Text="hello";
}

How to get two way binding with Template Binding in WPF

Template Binding by default doesnt support Two Way binding.
If you still need to have it, here's the workaround:

<TextBox Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}"/>

Tuesday, August 18, 2015