Binding.Converter Propiedad
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene o establece el objeto de convertidor al que llama el motor de enlace para modificar los datos a medida que se pasan entre el origen y el destino, o viceversa.
public:
property IValueConverter ^ Converter { IValueConverter ^ get(); void set(IValueConverter ^ value); };
IValueConverter Converter();
void Converter(IValueConverter value);
public IValueConverter Converter { get; set; }
var iValueConverter = binding.converter;
binding.converter = iValueConverter;
Public Property Converter As IValueConverter
<Binding Converter="converterReference"/>
Objeto IValueConverter que modifica los datos.
Para usar el convertidor en un enlace, cree primero una instancia de la clase de convertidor. En el ejemplo siguiente se muestra como un recurso en un archivo XAML.
<UserControl.Resources>
<local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
<TextBlock Grid.Column="0" Margin="5,0"
Text="{Binding Month, Converter={StaticResource Converter1}}"/>
// Custom class implements the IValueConverter interface.
public class DateToStringConverter : IValueConverter
{
#region IValueConverter Members
// Define the Convert method to change a DateTime object to
// a month string.
public object Convert(object value, Type targetType,
object parameter, string language)
{
// The value parameter is the data from the source object.
DateTime thisdate = (DateTime)value;
int monthnum = thisdate.Month;
string month;
switch (monthnum)
{
case 1:
month = "January";
break;
case 2:
month = "February";
break;
default:
month = "Month not found";
break;
}
// Return the month value to pass to the target.
return month;
}
// ConvertBack is not implemented for a OneWay binding.
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
#endregion
}
' Custom class implements the IValueConverter interface.
Public Class DateToStringConverter
Implements IValueConverter
' Define the Convert method to change a DateTime object to
' a month string.
Public Function Convert(ByVal value As Object, _
ByVal targetType As Type, ByVal parameter As Object, _
ByVal language As System.String) As Object _
Implements IValueConverter.Convert
' value is the data from the source object.
Dim thisdate As DateTime = CType(value, DateTime)
Dim monthnum As Integer = thisdate.Month
Dim month As String
Select Case (monthnum)
Case 1
month = "January"
Case 2
month = "February"
Case Else
month = "Month not found"
End Select
' Return the value to pass to the target.
Return month
End Function
' ConvertBack is not implemented for a OneWay binding.
Public Function ConvertBack(ByVal value As Object, _
ByVal targetType As Type, ByVal parameter As Object, _
ByVal language As System.String) As Object _
Implements IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
End Class
Cree un convertidor implementando la interfaz IValueConverter e implementando el método Convert . Ese método debe devolver un objeto que sea del mismo tipo que la propiedad de dependencia que el enlace tiene como destino, o al menos un tipo que se pueda convertir o convertir implícitamente en el tipo de destino.
Producto | Versiones |
---|---|
WinRT | Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100 |