Advanced programming


With triggers you program the dynamic behavior of a graphic element.

For every graphical property that you want to influence with triggers, you should first set the initial value in the "Style.Setters-Collectionper setter (see previous chapter).

To define triggers, open the trigger window with the context command "Edit Style Trigger":

 

 


Use data triggers to change the presentation of a graphic element when data in the model has changed.

Transferred to the iPlus framework this means: If the value of one or more properties of an ACComponent instance has changed, the display on the graphical user interface should change.
One possible solution is the use of converters, which was discussed in the chapter "Setting and converting properties". Converters have the disadvantage that methods have to be programmed and called for this, so the dynamization is not purely declarative in the XAML code.
With data triggers, dynamization can be done purely declaratively and is therefore the better choice.

We take our rectangle example again and change the color of the rectangle using data triggering:

  1. Press the "Data" button to add a data trigger to the style (1).
  2. Press the "One data item" button if the color change should only be from one property from the model (ACComponent) (2).
  3. Use "Drag & Drop" to move the corresponding property onto the VBContent text field (3). In our example, it is the SensorState property of the EMERGENCY STOP switch (or sensor).
  4. Select the value of the property at which the data trigger should become active in order to carry out its presentation change tasks, which are entered in either its setters list or the action lists .

 

In the example we only want to work with the setters list for the time being, because the separate chapter "Animations" is dedicated to actions. To add setters, switch to the "Setter" tab and use the "Add" button to add the graphical properties that the data trigger should change:

 

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

 

The "Binding" field of the data trigger has been bound to the ACUrl of the SensorState property. A data trigger can only bind one value. If several properties have to be taken into account, there are two different options. Either you use

which are discussed in more detail in the following sections.

 

 


With Multi Data triggers several attributes can be bound.

In our example, we would also like to take into account another property that should have an influence on the color change of the rectangle.

  1. To do this, press the "Multi-Data" button to add a multi-data trigger (1).
  2. Press the "One data item" button (for simple bindings) (2).
  3. Use "Drag & Drop" to move the properties that the multi-data trigger should take into account in the list box. In our example, it is the SensorState property of the EMERGENCY STOP switch and the RunState property (running message) of a motor (3). For each property you define the value of the property that is to be evaluated by the multi-data trigger so that the trigger is triggered.
  4. Switch to the "Setter" tab and use the "Add" button to add the graphic properties that the data trigger should change.

 

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

 

In the multi-data trigger, the conditions list was filled with two conditions. Each condition was linked to a property from the model (ACComponent). The multi-data trigger is only triggered when the value set under Value has occurred for both linked properties. Conditions are therefore always "AND" linked !

This AND link is also a disadvantage of multi-data triggers if an OR link or more complex logical expressions are required instead. One way out are data triggers with multibindings and expressions.

 


With a data trigger in combination with multibindings and expressions, bound properties with complex logical expressions can be evaluated.

For our rectangular example we proceed in the same way as for simple data triggers (see second section "Editing data triggers").

  1. Press the "Data" button to add a data trigger to the style (1). 
  2. Press the "More data items" button because the color change should only depend on several properties from the model (ACComponent) (2). 
  3. Use "Drag & Drop" to move the corresponding properties into the list box. In our example it is the SensorState property of the EMERGENCY STOP switch (3)
  4. as well as the RunState property (running message) of a motor (4).
  5. Open the editing window for the converter and select the "Expression" mode (5).
  6. Expression means that lambda expressions are to be used for the evaluation of the bound values. The lambda expressions are parsed with the "simproexpr" library. Technically, the lambda expression is translated into a so-called  anonymous function, which has the same number of parameters in the parameter list of the method signature according to the number of properties bound in the multibinding. In our example, the signature is
    (PANotifyState p1, Boolean p2) =>
    Then program the logical expression (6). For more information, read the instructions for the expression parser .
  7. The result of the lambda expression should always be a Boolean value. In the Value box, specify whether the trigger on false or true to trigger (7).

 

 

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

 

Essentially, the XAML code is similar to the code from the section "Setting and converting properties" because, on the one hand, multibinding was used in combination with converters. The difference, however, is that the logic using the expression syntax (expression in the converter property "Expression") is in the XAML code and the color change is using setter objects. The dynamization is therefore purely declarative without the use of method calls.

However, expressions reach their limits when logical expressions alone are not sufficient and, for example, database accesses have to be made or complete programming freedom is required.

In this case the "gip.core.layoutengine.ConverterBase" class is not used with expressions, but with method calls:

 


Use method calls when expressions reach their limits or you want to reuse expressions in different places.

Either declare a client-side method in your assembly or use script methods. In both cases the signature of the method must have as many transfer parameters as the number of properties bound in the multibinding.

In our example, the script method would look like this:

 

  1. Press the "New Client Method" button (1).
  2. Give the method a name in the "Method info" tab (2).
  3. Program the script method (3). In our example we named it "ConvertExampleA".

Now switch to the design where you want to use the method and open the trigger window to create a data trigger. Proceed as described in the previous section and repeat steps 1-4. Instead of step 5, in which lambda expressions were declared, we now need the method call. To do this, open the "Methods" expander and "Drag & Drop" the "ConvertExampleA" method onto the converter:

 

In the converter editor window, the mode was set to "ScriptEngine" and the address for calling the method is in the url. Close the trigger window and switch to the XAML editor. The following code was generated:  

 

Essentially, the XAML code is similar to the code from the section  "Setting and converting properties" because, on the one hand, multibinding was used in combination with converters. The difference, however, is that the method does not return an instance from the System.Windows or System-Drawing namespace. This creates a separation between model and presentation in accordance with the MVVM pattern,  because the presentation takes place declaratively using setter objects.