1, the introduction

For Windows desktop application development, WPF is praised for its special interface rendering and flexible interface layout, because it can provide users with better interactive experience. How do you use WPF to develop aesthetically pleasing interfaces and features? It is not just the layout that is responsible, but the flexible use of WPF templates, so here are the three main templates for WPF.

ControlTemplate 2

All WPF controls are based on the mode of separation of presentation and behavior. Developers can change the presentation state according to their own application scenarios, without affecting the original functional behavior of the control. It allows us to change the appearance of the control at will while retaining the control’s operation action. In addition to template separation, WPF routing events are also a benefit (I’ll talk about templates later, but I’ll talk about templates first). The following uses a control as an example to make different template changes.

Here we can directly use the window resource, create a ControlTemplate object, and render the result and code as follows:

The three buttons are rendered differently from template to template, but the original functionality remains intact

3. DataTemplate

Data templates are used in data set controls such as ItemsControl, ListView, ListBox, DataGrid, etc. The following is a simple example:

The DataTemplate specifies how the data in the data set bound to the ItemsControl should be displayed, and only the bound values can be displayed, not the unbound values

4, The container template ItemsPanelTemplate

The window template also comes with data collection controls, which specify how data is displayed. You can specify different layout controls to display data in different layouts. For example, the following example uses UniformGrid as an example

Sometimes PEOPLE ask me: How do I make a layout control dynamically add and delete items using bindings? In this example, you can change the container template of a data collection control to a container for dynamically adding and deleting items. Besides Grid and StackPanel, UniformGrid, WrapPanel, DockPanel and Canvas can be used flexibly to achieve unexpected effects.

Finally, we show a custom container approach for dynamic data rendering of a waterfall layout:

Keep watching and update more free resources in the future!