Using Transformers
Transformers are objects that provide a conversion from one value type to another. They implement the ITransformer interface. To use any of these transformers, the target must accept the type of value that is returned.
The following transformer elements are used to convert data:
- The BooleanTransformer element converts data to a Boolean value. A common use for this conversion is for setting the Visible property for a view item; the UI is only displayed in a non-zero or non-null case.
- The DateTimeTransformer element converts a date-time value to a string for display purposes. The formats you can use are members of the DateTimeFormats enumeration.
- The FormatTransformer element converts an object to a string with a format that you specify. For example, you could display "4255550123" as a telephone number format such as "(425) 555-0123". For more information about extended format strings, see the Remarks section of the String.Format Method on the MSDN web site.
- The MathTransformer element performs a mathematical conversion on a numeric value by applying math operators (multiply, divide, add, subtract, modulo, absolute, and sign—in that order, regardless of the order you specify them). You can also apply a type conversion to the result.
- The TimeSpanTransformer element transforms a TimeSpan value to a string for display purposes. The formats you can use are members of the TimeSpanFormats enumeration. For more information about TimeSpan format, see the TimeSpan Structure on the MSDN web site.
You cannot perform one conversion after another on the same variable in a chain. To perform multiple conversions, you can transform the value to an intermediate local and then bind that value to your target using another transformer, or use multiple actions on the same variable using a rule.
The example below shows how to perform two transforms on the same variable <cor:Double Name="PhoneNumber" Double="4255551211"/>
. The first one adds "1" to the phone number, and the second transform formats the number.
<Rule>
<Actions>
<Set Target="[PhoneNumber]" Value="[PhoneNumber]">
<Transformer>
<MathTransformer Add="1" />
</Transformer>
</Set>
<Set Target="[Label.Content]" Value="[PhoneNumber]">
<Transformer>
<FormatTransformer Format="The phone number is {0}."
ExtendedFormat="(###) ###-####" />
</Transformer>
</Set>
</Actions>
</Rule>
Sample Explorer
- Transformers > all samples
See Also