Value Converters
The ActiproSoftware.Windows.Data, ActiproSoftware.Windows.Controls, ActiproSoftware.Windows.Media namespaces contain numerous value converters that can be used in XAML for working with data.
BooleanAndConverter Class
The BooleanAndConverter represents a multi-value converter that performs a logical AND (&&
) operation on one or more Boolean
values passed to the associated MultiBinding
object.
BooleanNotConverter Class
The BooleanNotConverter represents a value converter that performs a NOT (!
) operation on a specified Boolean
value.
BooleanOrConverter Class
The BooleanOrConverter represents a multi-value converter that performs a logical OR (||
) operation on one or more Boolean
values passed to the associated MultiBinding
object.
BorderChildClipConverter Class
The BorderChildClipConverter multi-value converter can be applied to a Border.Child
element's Clip
property to ensure the child element is properly clipped based on the Border.CornerRadius
value.
This is useful when the Border.Child
element or another element within its hierarchy has a background that renders into the portion of the parent Border
that is normally rounded off by the corner radius.
Four values must be passed into the converter using a MultiBinding
. The first two are the width and height of the child element. The third is the BorderThickness
of the parent Border
. The fourth is the CornerRadius
of the parent Border
.
This XAML shows an example of using the converter:
<Border x:Name="outerBorder" BorderBrush="Black" BorderThickness="1" CornerRadius="8">
<Grid>
<Grid.Clip>
<MultiBinding Converter="{StaticResource BorderClipConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth" />
<Binding RelativeSource="{RelativeSource Self}" Path="ActualHeight" />
<Binding ElementName="outerBorder" Path="BorderThickness" />
<Binding ElementName="outerBorder" Path="CornerRadius" />
</MultiBinding>
</Grid.Clip>
<!-- Grid child controls here -->
</Grid>
</Border>
BrushOpacityConverter Class
The BrushOpacityConverter represents a value converter that clones a specified Brush
and updates the Brush.Opacity
property based on the converter's parameter.
CharacterCasingConverter Class
The CharacterCasingConverter represents a value converter that can be used to change a string's character casing to uppercase or lowercase. Set the CharacterCasingConverter.CharacterCasing property to either Upper
(the default) or Lower
to specify the casing mode.
CoalesceConverter Class
The CoalesceConverter represents a value converter that returns the value if it is non-null
; otherwise, the value of the converter's parameter. This converter can also be used in a MultiBinding
, in which case the first non-null
value is returned; otherwise, the converter's parameter is returned.
ColorInterpolationConverter Class
The ColorInterpolationConverter can be used in XAML bindings to return the linear Color
value that is the specified percentage between the value of two Color
objects.
The optional percentage is indicated in the converter parameter. If not specified, the default of 0.5
(50%) will be used.
Exactly two Color
objects must be passed into the converter using a MultiBinding
.
This XAML shows how to use the converter to find a color that is between the system control and window colors but 90% towards the window color:
<MultiBinding Converter="{StaticResource ColorInterpolationConverter}" ConverterParameter="0.9">
<Binding Source="{x:Static SystemColors.ControlColor}" />
<Binding Source="{x:Static SystemColors.WindowColor}" />
</MultiBinding>
ConditionalConverter Class
The ConditionalConverter represents a value and multi-value converter that provides if-else functionality for Binding
and MultiBinding
objects.
This converter expects the following source values to be specified:
- Condition - A boolean value indicating whether the true or false value should be returned.
- TrueValue - An object that is returned when the Condition is
true
. This can be omitted if the FalseValue parameter is also omitted, in which case the value of theTrueValue
property will be used. - FalseValue - An object that is returned when the Condition is
false
. This can be omitted, in which case the value of theFalseValue
property will be used.
This sample code shows how to use the converter in a MultiBinding
in XAML:
<MultiBinding Converter="{StaticResource ConditionalConverter}">
<Binding Path="IfConditionValue" />
<Binding Path="TrueValue" />
<Binding Path="FalseValue" />
</MultiBinding>
This sample code shows how to use the converter in a Binding
in XAML:
<shared:ConditionalConverter x:Key="ConditionalConverter" TrueValue="The Value is true" FalseValue="The Value is false" />
...
<TextBlock Text="{Binding Path=BooleanProperty, Converter={StaticResource ConditionalConverter}}" />
If the condition is not a boolean, other condition evaluation logic executes as follows. Strings evaluate true
if they are not null
or empty. Non-null
objects also return true
.
CornerRadiusConverter Class
The CornerRadiusConverter represents a value converter that converts between a CornerRadius
and a number.
<... Value="{Binding Path=Number, Converter={StaticResource CornerRadiusConverter}}" ... />
DelegateConverter Class
The DelegateConverter represents a value converter that uses delegates to perform the underlying conversion to/from the source. Set the ConvertCallback and ConvertBackCallback properties to the delegates to use for conversion and back conversion respectively.
DurationToMillisecondConverter Class
The DurationToMillisecondConverter represents a value converter that converts between a Duration
and a number of milliseconds.
EnumDescriptionConverter Class
The EnumDescriptionConverter represents a value converter that uses the DescriptionAttribute
for the string representation of the enumeration values, when available.
ImageConverter Class
The ImageConverter represents a value converter that returns a new DynamicImage (inherits Image
) control instance created using a specified URI or BitmapSource
.
This converter expects the source value to be a Uri
, a URI String
, or a BitmapSource
that can be used to create a new DynamicImage instance. A prefix can be defined in UriPrefix which will be prepended to all source values of type String
before the DynamicImage is created.
The Width
and Height
properties can optionally be set to set the related properties on the DynamicImage control that is created.
The ImageProvider property can optionally be set to a specific ImageProvider to assign to the ImageSource
created by the converter. Leave the property its default value of null
to use the static default ImageProvider instance.
ImageKeyToImageSourceConverter Class
The ImageKeyToImageSourceConverter represents a value converter that uses an ImageProvider to lookup an ImageSource
associated with a specified key.
This converter expects the source value to be a String
that is a key recognized by ImageProvider.
Optionally set the converter parameter to a specific instance of ImageProvider. Otherwise, the instance defined by ImageProvider.Default will be used.
<... ImageSource="{Binding Source={x:Static shared:SharedImageSourceKeys.Warning}, Converter={StaticResource ImageKeyToImageSourceConverter}}" ... />
InverseConverter Class
The InverseConverter represents a value converter that inverts another value converter. Set an instance of the IValueConverter
to invert in the Converter property.
IsEnumFlagSetConverter Class
The IsEnumFlagSetConverter represents a value converter that returns whether the specified enumeration value has the flag, specified by the converter's parameter, set.
IsNullOrEmptyConverter Class
The IsNullOrEmptyConverter represents a value converter that returns whether the specified value is null
, and if it is a string, also if it is null
or empty.
IsTypeConverter Class
The IsTypeConverter represents a value converter that returns whether the specified object is the Type
indicated in the converter's parameter.
MultiplicationConverter Class
The MultiplicationConverter represents a single and multi-value converter that multiplies all the source values provided, and optionally the converter's parameter.
NoOpConverter Class
The NoOpConverter represents a value converter that does not alter the value in any way (e.g., no operation).
This converter can be used to bypass an issue that arises from an optimization present in the WPF binding system.
ParallaxConverter Class
The ParallaxConverter represents a value converter that can be used to create a parallax background scrolling effect.
Use the converter in a binding on a background element's RenderTransform
's TranslateTransform.X
or Y
property, and bind to a related ScrollViewer.HorizontalOffset
or VerticalOffset
property.
PercentageConverter Class
The PercentageConverter represents a value converter that converts between a number and a percentage. The percentage is simply the number multiplied by 100
.
StringFormatConverter Class
The StringFormatConverter represents a multi-value converter that provides String.Format
functionality for both simple Binding
and MultiBinding
objects.
Pass the format string in the converter's parameter. Since curly brace characters {
and }
are normally interpreted as markup extension starts, you can escape them by placing "{}"
before the format string like this:
<MultiBinding Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}{0} of {1}">...
Important
If the converter's parameter is undefined, the first value of the multi-binding must define the string format. This is useful when the string format needs to be configured with a Binding
since WPF does not support bindings for converter parameters.
ThicknessConverter Class
The ThicknessConverter represents a value converter that converts between a Thickness
and a number.
By default, the Thickness
returned will have all four sides (Thickness.Left
, Thickness.Top
, Thickness.Right
, and Thickness.Bottom
) set to the specified number. An optional parameter of type Sides can be specified, which can be used to customize the sides that are set.
<!-- Set all four sides -->
<... Value="{Binding Path=Number, Converter={StaticResource ThicknessConverter}}" ... />
<!-- Only set the Left and Right sides -->
<... Value="{Binding Path=Number, Converter={StaticResource ThicknessConverter}, ConverterParameter='Left,Right'}" ... />
TypeNameConverter Class
The TypeNameConverter represents a value converter that converts a value to a String
of its short type name.
UnitToDoubleConverter Class
The UnitToDoubleConverter represents a value converter that converts between a Unit and a Double
.
This converter needs additional information in order to convert a Double
into a Unit. Specifically, it needs to the know whether the value is measured in pixels or as a percentage. To accommodate this need, the property Type is used to specify the type of measurement.
UriConverter Class
The UriConverter represents a value converter that returns a new Uri
instance created using the specified URI string combined with an optional URI prefix.
This converter expects the source value to be a String
which, when combined with the UriPrefix, can be used to create a new Uri
instance.
UserPromptUIDialogButtonTextConverter Class
The UserPromptUIDialogButtonTextConverter represents a value converter that translates a UserPromptStandardResult into the equivalent text which can describe that button in a user interface dialog.
ZoomLevelToTextFormattingModeConverter Class
The ZoomLevelToTextFormattingModeConverter represents a value converter that alters the TextFormattingMode
based on the specified zoom level.
Display
mode is used when the zoom level is 1.0
. Ideal
mode is used when the zoom level is increased.