Binding.Converter Propiedad

Definición

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.

C#
public IValueConverter Converter { get; set; }
XAML
<Binding Converter="converterReference"/>

Valor de propiedad

Objeto IValueConverter que modifica los datos.

Ejemplos

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.

XAML
<UserControl.Resources>
  <local:DateToStringConverter x:Key="Converter1"/>
</UserControl.Resources>
XAML
<TextBlock Grid.Column="0" Margin="5,0"
  Text="{Binding Month, Converter={StaticResource Converter1}}"/>
C#
// 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
}

Comentarios

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.

Se aplica a

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

Consulte también