I learned about the WPF style and template chapter. The book first talks about the style, and then about the template. According to the examples in the book, the specified effect is successfully achieved. Then I learned how to use styles and templates in combination. This is a problem.
Only the label nodes of the template are provided in the book, and the detailed code of the style and style reference template (for the Button control ). I introduced the code to the window resource MainWindow. Resources, and added a Button in the Grid to reference one of the styles (two styles apply one template ). After running, the window is blank (because there is no code in the template). I think it should be a problem with the template code. Because the template code is incomplete, I will refer to the previous template chapter, added the template code. In this case, the Background attribute value in the style does not work. I thought about it for a moment. It turned out that it was easy to figure and directly made a Rectangle in the template, and filled the specified color with the Fill attribute. Looking back at the template chapter, it focuses on two restrictions on template binding: 1. Only within the template's visual tree; 2. It cannot be applied to the attributes of a Freezable derived object, for example, binding the Color attribute of the Brush fails. The second restriction should be solved using regular data Binding, so I added the following attribute to the Gradient Tag: Color = "{Binding RelativeSource = {RelativeSourceTemplateParent}, Path = Background. Color} "there is a problem with the data binding (note the bold part). As a result, I run the command and the system reports an exception-the" RelativeSourceMode "cannot be created from the text" TemplateParent ". This error was made once when I learned the template. However, after searching for another half a day, I found that the correct one is:TemplatedParent, Pay attention to the red d. After this operation, it will be displayed normally. If you switch to another style, the background color will be the color of another style.
The following points are summarized:
1. The style can be x: Key or not. If no, you must specify TargetType. Sometimes, you must specify the x: Key value in the Button. If no TargetType exists, you must specify the type before the attribute name, such as Button. background.
2. If the template is not set, the system automatically calls the system template. Each control of the system is a complete template, set the IsMouseOver attribute value (for example, yellow) of the button with the style trigger. However, after running the trigger, when the mouse is placed on the button, the color will flash yellow, and then the color will remain light blue, this is because the system template is playing a strange role. To remove this effect, one of the methods I currently know is to write a button template by myself (the style has a higher priority than the template, this is the priority of the custom style and template, but the default Template Effect of this system cannot be overwritten by the custom style effect. I do not know why ).
3. If the template is set, you must write code in it, especially in ViewBox. <ContentPresenter Margin = "20" Content = "{TemplateBinding Content}"/>, otherwise, your control will not be rendered (note: the button is not invisible, but is not generated at all, which is different from setting the button to transparent ).
4. Pay attention to data binding: the circle or Rectangle in your button is determined by <Ellipse/> and <Rectangle/> in your template. You can add more complex images, for example, the example used in the template section is to enclose two concentric circles for presentation, and then set the Fill attribute in the image. In this example, linear painter LinearGradientBrush is used, gradientStop is also used in the paint brush. I still know how to use the paint brush, but the Color binding is like this. If you set the Color here to Red or other colors, if you want to set the background color externally (such as a style or a specific button), use this Binding: {Binding RelativeSource = {RelativeSourceTemplateDParent}, Path = Background. Color}. Be sure to note that it is TemplatedParent. Otherwise, an error will be reported.
5. Finally, the book also says that templates have disadvantages. After you define a template, all the original system attributes of the control disappear, for example, the buttons become grayed out after being disabled, therefore, if you decide to customize the control template, you need to be responsible for all the actions of the control, and ultimately achieve the desired effect.
Finally, I would like to say a few words. Here, I will learn WPF's usage book: "sunflower collection-WPF self-taught manual" [Li Xiang] By {e-Industry Press}. I suggest you read it. In addition, there is no code here, because this essay is not a tutorial, but a result of learning the notes that are not carefully taken. But I believe that if you use the sunflower collection... Chapter 1 of the book: after reading the style and control template, you can probably guess the Code involved, the reference code is "code after code 13-17", "program after code 13-20", and "code 13-21 without modifying the template, just adding a style ".