Binding.Converter Propriété

Définition

Obtient ou définit l’objet de conversion appelé par le moteur de liaison pour modifier les données à mesure qu’elles sont passées entre la source et la cible, ou inversement.

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"/>

Valeur de propriété

Objet IValueConverter qui modifie les données.

Exemples

Pour utiliser votre convertisseur dans une liaison, commencez par créer un instance de votre classe de convertisseur. L’exemple suivant montre cette ressource dans un fichier 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

Remarques

Créez un convertisseur en implémentant l’interface IValueConverter et en implémentant la méthode Convert . Cette méthode doit retourner un objet qui est du même type que la propriété de dépendance que la liaison cible, ou au moins un type qui peut être implicitement contraignant ou converti en type cible.

S’applique à

Voir aussi