Il presente articolo è stato tradotto manualmente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale. |
Traduzione
Originale
|
Metodo IValueConverter.Convert
Converte un valore.
Assembly: PresentationFramework (in PresentationFramework.dll)
Object Convert( Object value, Type targetType, Object parameter, CultureInfo culture )
Parametri
- value
- Tipo: System.Object
Valore prodotto dall'origine dell’associazione.
- targetType
- Tipo: System.Type
Tipo della proprietà origine dell’associazione.
- 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.ObjectUn valore convertito. Se il metodo restituisce Nothing, viene utilizzato il valore null valido.
Il motore di associazione dei dati richiama questo metodo quando effettua la propagazione di un valore dall’origine dell’associazione alla destinazione dell’associazione.
Il modulo di associazione dati non rileva eccezioni generate da un convertitore fornito dall'utente. Eventuali eccezioni generate dal metodo Convert, o qualsiasi eccezione non intercettata generata dai metodi chiamati dal metodo Convert, 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.
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:
[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.
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.
<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.
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.
