Search code examples
c#.netwpfxaml

Difference between Style and ControlTemplate


Could you tell me what is the main differences between Style and ControlTemplate? When or why to use one or the other ?

To my eyes, they are exactly the very same. As I am beginner I think that I am wrong, thus my question.


Solution

  • You can think of a Style as a convenient way to apply a set of property values to more than one element. You can change the default appearance by setting properties, such as FontSize and FontFamily, on each TextBlock element directly. However, if you want your TextBlock elements to share some properties, you can create a Style in the Resources section of your XAML file.

    On the other hand, a ControlTemplate specifies the visual structure and visual behavior of a control. You can customize the appearance of a control by giving it a new ControlTemplate. When you create a ControlTemplate, you replace the appearance of an existing control without changing its functionality. For example, you can make the buttons in your application round instead of the default square shape, but the button will still raise the Click event.

    Ref: http://msdn.microsoft.com/en-us/library/ms745683.aspx