Install Silverlight template from code?

How can I set control.Template from code if my template is placed in a ResourceDictionary?

+3
source share
1 answer

Potentially, you need to attach to an event loaded with control. At this point, you can assign the Template property. You can get the template from the resource dictionary.

For example, suppose you have a UserControl that contains a TextBox that you want to provide for another template in the UserControl code, and that the template is stored in the UserControls Resources property.

<UserControl xmlns="Namespaces removed for clarity" >
  <UserControl.Resources>
     <ControlTemplate TargetType="TextBox" x:Key="MyTextBox">
       <!-- template mark up here -->
     </ControlTemplate>
  <UserControl.Resources>
  <TextBox x:Name="txt" Loaded="txt_loaded" />
</UserControl>

In the code for UserControl you will have this code: -

void txt_Loaded(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Template = (ControlTemplate)Resources["MyTextBox"];
}

, , UserControls.

void Page_Loaded(object sender, RoutedEventArgs e)
{
    txt.Template = (ControlTemplate)Resources["MyTextBox"];
}
+9

Source: https://habr.com/ru/post/1727096/


All Articles