Converts a value.
Assembly: PresentationFramework (in PresentationFramework.dll)
Function Convert ( _
value As [%$TOPIC/ms590771_en-us_VS_110_1_0_0_0_0%], _
targetType As [%$TOPIC/ms590771_en-us_VS_110_1_0_0_0_1%], _
parameter As [%$TOPIC/ms590771_en-us_VS_110_1_0_0_0_2%], _
culture As [%$TOPIC/ms590771_en-us_VS_110_1_0_0_0_3%] _
) As [%$TOPIC/ms590771_en-us_VS_110_1_0_0_0_4%]
[%$TOPIC/ms590771_en-us_VS_110_1_0_1_0_0%] Convert(
[%$TOPIC/ms590771_en-us_VS_110_1_0_1_0_1%] value,
[%$TOPIC/ms590771_en-us_VS_110_1_0_1_0_2%] targetType,
[%$TOPIC/ms590771_en-us_VS_110_1_0_1_0_3%] parameter,
[%$TOPIC/ms590771_en-us_VS_110_1_0_1_0_4%] culture
)
[%$TOPIC/ms590771_en-us_VS_110_1_0_2_0_0%]^ Convert(
[%$TOPIC/ms590771_en-us_VS_110_1_0_2_0_1%]^ value,
[%$TOPIC/ms590771_en-us_VS_110_1_0_2_0_2%]^ targetType,
[%$TOPIC/ms590771_en-us_VS_110_1_0_2_0_3%]^ parameter,
[%$TOPIC/ms590771_en-us_VS_110_1_0_2_0_4%]^ culture
)
abstract Convert :
value:[%$TOPIC/ms590771_en-us_VS_110_1_0_3_0_0%] *
targetType:[%$TOPIC/ms590771_en-us_VS_110_1_0_3_0_1%] *
parameter:[%$TOPIC/ms590771_en-us_VS_110_1_0_3_0_2%] *
culture:[%$TOPIC/ms590771_en-us_VS_110_1_0_3_0_3%] -> [%$TOPIC/ms590771_en-us_VS_110_1_0_3_0_4%]
Parameters
- value
- Type:
SystemObject
The value produced by the binding source.
- targetType
- Type:
SystemType
The type of the binding target property.
- parameter
- Type:
SystemObject
The converter parameter to use.
- culture
- Type:
System.GlobalizationCultureInfo
The culture to use in the converter.
Return Value
Type: SystemObjectA converted value. If the method returns , the valid null value is used.
The data binding engine calls this method when it propagates a value from the binding source to the binding target.
The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyPropertyUnsetValue.
A return value of DependencyPropertyUnsetValue indicates that the converter produced no value and that the binding uses the FallbackValue, if available, or the default value instead.
A return value of BindingDoNothing indicates that the binding does not transfer the value or use the FallbackValue or default value.
This example shows how to apply conversion to data that is used in bindings.
To convert data during binding, you must create a class that implements the IValueConverter interface, which includes the Convert and ConvertBack methods.
The following example shows the implementation of a date converter that converts the date value passed in so that it only shows the year, the month, and the day. When implementing the IValueConverter interface, it is a good practice to decorate the implementation with a ValueConversionAttribute attribute to indicate to development tools the data types involved in the conversion, as in the following example:
Public Class DateConverter
Implements System.Windows.Data.IValueConverter
Public Function Convert(ByVal value As Object,
ByVal targetType As System.Type,
ByVal parameter As Object,
ByVal culture As System.Globalization.CultureInfo) _
As Object Implements System.Windows.Data.IValueConverter.Convert
Dim DateValue As DateTime = CType(value, DateTime)
Return DateValue.ToShortDateString
End Function
Public Function ConvertBack(ByVal value As Object,
ByVal targetType As System.Type,
ByVal parameter As Object,
ByVal culture As System.Globalization.CultureInfo) _
As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Dim strValue As String = value
Dim resultDateTime As DateTime
If DateTime.TryParse(strValue, resultDateTime) Then
Return resultDateTime
End If
Return DependencyProperty.UnsetValue
End Function
End Class
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return DependencyProperty.UnsetValue;
}
}
Once you have created a converter, you can add it as a resource in your Extensible Application Markup Language (XAML) file. In the following example, src maps to the namespace in which DateConverter is defined.
<src:DateConverter x:Key="dateConverter"/>
Finally, you can use the converter in your binding using the following syntax. In the following example, the text content of the TextBlock is bound to StartDate, which is a property of an external data source.
<TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
Name="startDateTitle"
Style="{StaticResource smallTitleStyle}">Start Date:</TextBlock>
<TextBlock Name="StartDateDTKey" Grid.Row="2" Grid.Column="1"
Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}"
Style="{StaticResource textStyleTextBlock}"/>
The style resources referenced in the above example are defined in a resource section not shown in this topic.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.