[ACCEPTED]-How to set Control Template in code?-contentcontrol

Accepted answer
Score: 34

Creating template in codebehind is not a 26 good idea, in theory one would do this by 25 defining the ControlTemplate.VisualTree which is a FrameworkElementFactory.

ControlTemplate template = new ControlTemplate(typeof(Button));
var image = new FrameworkElementFactory(typeof(Image));
template.VisualTree = image;

Assigning properties 24 is very roundabout since you need to use 23 SetValue and SetBinding:

image.SetValue(Image.SourceProperty, ...);

Also, about the (previously) accepted 22 answer and the stuff quoted:

Setting the 21 ControlTemplate programmatically is just 20 like using XAML because we have to use 19 the XamlReader class.

That statement is 18 just wrong, we do not "have to".


If i assign templates 17 at run time i define them as a resource 16 which i can load if i need it.


Edit: According 15 to the documentation FrameworkElementFactory is deprecated:

This 14 class is a deprecated way to programmatically 13 create templates, which are subclasses of 12 FrameworkTemplate such as ControlTemplate 11 or DataTemplate; not all of the template 10 functionality is available when you create 9 a template using this class. The recommended 8 way to programmatically create a template 7 is to load XAML from a string or a memory 6 stream using the Load method of the XamlReader 5 class.

I wonder if this recommendation is 4 such a good idea. Personally i would still 3 go with defining the template as a resource 2 in XAML if i can avoid doing it with strings 1 and the XamlReader.

Score: 25

http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/73fdb6a2-6044-4c43-8766-afa12618ddc1/set-controltemplate-programmatically.aspx

Setting the ControlTemplate programmatically 15 is just like using XAML because we have 14 to use the XamlReader class. For example, here 13 is the code to set a button's template, assuming 12 that we want to set a button's template 11 after it is loaded.

private void Button_Loaded(object sender, RoutedEventArgs e) {
    var button = sender as Button;
    string template =
        "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            TargetType=\"Button\">" +
            "<Border>" +
                 "<ContentPresenter/>" +
            "</Border>" +
        "</ControlTemplate>";
    button.Template = (ControlTemplate)XamlReader.Parse(template);
}

Since we used a string 10 for specifying the XAML code for the template, we 9 can use the XamlReader's Parse method. The XamlReader 8 also has a Load method, which is primarily 7 used for streams or XAML or XML readers. Notice 6 that we have to include the XML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation because 5 the ControlTemplate, Border, and other 4 controls we need are defined there. If 3 we did not include it, we'll encounter 2 a runtime exception. Basically, we have 1 to put the namespaces needed by the template.

Score: 0

If you need to change the button image only 4 then you can do one thing.

  1. Create a dependency property which will represent when you want to change the image (a bool) or may be you can create an enum which has all possible images say
  2. Enum Images { Image1 = 0, Image2 = 1, Image2 = 3}. Create a dependency property "CurrentButtonImage" of this type which will be associated with button.

Now in XAML 3 use this in button template

On property 2 Change of CurrentButtonImage update the 1 image of button (in code behind) using

CurrentImagePropertyChangedhandler(....,...)  
{  
    switch(CurrentButtonImage)  
    {  
        case "Image1" :  
          this._ButtonImage.Fill = (DrawingBrush)csd.FindResource("Image1DrawingBrush");
          break;
    }
}

More Related questions