In this case, you create new ContentControl
, but do not add to the current VisualTree, respectively, it is not visible.
Also, there is no property testControl
in a UserControl, because .
symbol used for access to the properties of Class, therefore remove m
before testControl
or use this
instead:
UserControl m = sender as UserControl;
Style s = m.Resources["test"] as Style;
m.testControl = new ContentControl(); // Remove this line
m.testControl.Style = s; // and 'm', or write like this: this.testControl.Style = s;
m.testControl.ApplyTemplate();
And the final result is:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var control = sender as UserControl;
if (control != null)
{
Style s = control.Resources["test"] as Style;
testControl.Style = s;
// control.ApplyTemplate(); // it's not necessary in your case
}
}