ConditionalConverter Class
Represents a value and multi-value converter that provides if-else functionality for Binding
and MultiBinding
objects.
public class ConditionalConverter
- Inheritance:
- object object
Remarks
This converter expects the following source values to be specified:
- Condition - A bool 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.
Examples
<MultiBinding Converter="{StaticResource ConditionalConverter}">
<Binding Path="IfConditionValue" />
<Binding Path="TrueValue" />
<Binding Path="FalseValue" />
</MultiBinding>
<shared:ConditionalConverter x:Key="ConditionalConverter" TrueValue="The Value is true" FalseValue="The Value is false" />
...
<TextBlock Text="{Binding Path=BooleanProperty, Converter={StaticResource ConditionalConverter}}" />
Constructors
ConditionalConverter()
Initializes an instance of the class.
public ConditionalConverter()
Properties
ConditionValue
Gets or sets the optional value to use for the conditional test.
public object ConditionValue { get; set; }
Property Value
- object:
The optional value to use for the conditional test.
FalseValue
Gets or sets the value returned when the conditional test is false
.
public object FalseValue { get; set; }
Property Value
- object:
The value returned when the conditional test is
false
.
TrueValue
Gets or sets the value returned when the conditional test is true
.
public object TrueValue { get; set; }
Property Value
- object:
The value returned when the conditional test is
true
.
Methods
Convert(object, Type, object, CultureInfo)
Converts a value.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Parameter | Type | Description |
---|---|---|
value | object | The value produced by the binding source. |
targetType | Type | The type of the binding target property. |
parameter | object | The converter parameter to use. |
culture | CultureInfo | The culture to use in the converter. |
Returns
- object:
A converted value. If the method returns null, the valid null value is used.
Convert(object[], Type, object, CultureInfo)
Returns either the second or third source value, based on the first source value.
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
Parameter | Type | Description |
---|---|---|
values | object[] | The array of values that the source bindings in the MultiBinding produces. |
targetType | Type | The type of the binding target property. |
parameter | object | The converter parameter to use. |
culture | CultureInfo | The culture to use in the converter. |
Returns
- object:
The second source value, if the first source value equals
true
; otherwise the third source value.
Remarks
See ConditionalConverter for more information on the expected source values.
ConvertBack(object, Type, object, CultureInfo)
Converts a value.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Parameter | Type | Description |
---|---|---|
value | object | The value that is produced by the binding target. |
targetType | Type | The type to convert to. |
parameter | object | The converter parameter to use. |
culture | CultureInfo | The culture to use in the converter. |
Returns
- object:
A converted value. If the method returns null, the valid null value is used.
ConvertBack(object, Type[], object, CultureInfo)
This method always returns null
and should not be used.
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
Parameter | Type | Description |
---|---|---|
value | object | Not used. |
targetTypes | Type[] | Not used. |
parameter | object | Not used. |
culture | CultureInfo | Not used. |
Returns
- object[]:
null
.