Metodo IValueConverter.ConvertBack (System.Windows.Data)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Metodo IValueConverter.ConvertBack
Il presente articolo è stato tradotto manualmente. Per visualizzare questa pagina e contemporaneamente visualizzarne il contenuto in lingua inglese, passare alla visualizzazione semplificata.

Converte un valore.

Spazio dei nomi:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
Sintassi

Visual Basic
Function ConvertBack ( _
	value As Object, _
	targetType As Type, _
	parameter As Object, _
	culture As CultureInfo _
) As Object
C#
Object ConvertBack(
	Object value,
	Type targetType,
	Object parameter,
	CultureInfo culture
)
Visual C++
Object^ ConvertBack(
	Object^ value, 
	Type^ targetType, 
	Object^ parameter, 
	CultureInfo^ culture
)
F#
abstract ConvertBack : 
        value:Object * 
        targetType:Type * 
        parameter:Object * 
        culture:CultureInfo -> Object 

Parametri

value
Tipo: System.Object
Valore prodotto dalladestinazione dell’associazione.
targetType
Tipo: System.Type
Tipo in cui eseguire la conversione.
parameter
Tipo: System.Object
Il parametro del convertitore da utilizzare.
culture
Tipo: System.Globalization.CultureInfo
Le impostazioni cultura da utilizzare nel convertitore.

Valore restituito

Tipo: System.Object
Un valore convertito. Se il metodo restituisce null, viene utilizzato il valore null valido.
Note

Il motore di associazione dei dati richiama questo metodo quando effettua la propagazione di un valore dall’origine dell’associazione alla destinazione dell’associazione.

L'implementazione di questo metodo deve essere l'inverso del metodo Convert.

Il modulo di associazione dati non rileva eccezioni generate da un convertitore fornito dall'utente. Eventuali eccezioni generate dal metodo ConvertBack, o qualsiasi eccezione non intercettata generata dai metodi chiamati dal metodo ConvertBack, vengono considerate come errori di runtime. Gestire i problemi previsti restituendo DependencyProperty.UnsetValue.

Un valore restituito di DependencyProperty.UnsetValue indica che il convertitore non ha prodotto alcun valore e che l'associazione utilizza FallbackValue, se disponibile, o invece il valore predefinito.

Un valore restituito di Binding.DoNothing indica che l'associazione non trasferisce il valore o utilizza FallbackValue o il valore predefinito.

Esempi

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:

C#

[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.

XAML

<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.

XAML

<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.

Informazioni sulla versione

.NET Framework

Supportato in: 4, 3.5, 3.0

.NET Framework Client Profile

Supportato in: 4, 3.5 SP1
Piattaforme

Windows 7, Windows Vista SP1 o versione successiva, Windows XP SP3, Windows Server 2008 (componenti di base del server non supportati), Windows Server 2008 R2 (componenti di base del server supportati con SP1 o versione successiva), Windows Server 2003 SP2

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.
Vedere anche

Riferimenti

Altre risorse