Tuesday, July 3, 2012

WPF Access the child control of a user control

This was a question asked here. This is a situation when we've created a custom user control and when we add it to a parent user control, we can no longer access the child user control's UI components.  I'd answered it there, but wanted to add this here as it would be helpful.

Define properties in the child's class for each of those controls. You will be able to access them from the Parent User Control, assuming you have added the Child User Control within the Parent User Control.

Parent User Control.. SingalData is the child User Contol
<my:C1TabItem Header="Signal"> <local:SignalData Width="1036"></local:SignalData> </my:C1TabItem> 

In the Child User Contorl class, if you have a component named tabProductList you add a property -
public C1.WPF.C1TabControl TabProductList { get { return this.tabProductList; } } 

And finally, from your parent class you can reference it as -
C1TabItem tbItem = (C1TabItem)c1TabControl1.SelectedItem;
SignalData sigInp = (SignalData)tbItem.Content;
if (sigInp.TabProductList.SelectedIndex == 0)
{
....
}