Binding.Converter Proprietà

Definizione

Ottiene o imposta l'oggetto convertitore chiamato dal motore di associazione per modificare i dati mentre vengono passati tra l'origine e la destinazione 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"/>

Valore della proprietà

Oggetto IValueConverter che modifica i dati.

Esempio

Per usare il convertitore in un'associazione, creare prima di tutto un'istanza della classe del convertitore. Nell'esempio seguente viene illustrato come risorsa in un file 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

Commenti

Creare un convertitore implementando l'interfaccia IValueConverter e implementando il metodo Convert . Tale metodo deve restituire un oggetto con lo stesso tipo della proprietà di dipendenza a cui è destinata l'associazione o almeno un tipo che può essere eseguito in modo implicito o convertito nel tipo di destinazione.

Si applica a

Vedi anche