Advanced programming


In the previous chapter "Setting & converting properties" you learned how to set a dependency property directly or change it using converters if properties in the model (or one or more ACComponent instances) change.

However, the converter technology has the disadvantage that methods have to be provided which carry out a type conversion. This can be circumvented using other WPF techniques. One of them is the use of styles and triggers , which will be discussed in the following subsections.

But you have to decide which technique you want to use. Both cannot be used at the same time! In the case of the "rectangle example", this means that you must not set the "Fill" property directly, but indirectly by using the setter class  within a style.

The first step is therefore always to define a style by opening the triangle at the top right and calling the "Edit Style Setter" command:


The style window opens, in which you can then set the graphic properties. Select a property from the list in the left half of the window. Use the "Add" button to apply the property to the style. You can then set the value of the property in the list on the right. 

For our rectangle example, take the Fill property and then change the fill color:

 

Close the style window and switch to the XAML editor. The following code was generated:

 

The Fill property is no longer set directly, but by means of the setter declaration. Styles and setters are the prerequisite for triggers. For this reason, you should first add any properties that you want to change dynamically with triggers in the "Style.Setters Collection".