Rappresenta un attributo che consente all'autore di un convertitore di valore di specificare i tipi di dati interessati nell'implementazione del convertitore.
System.Attribute
System.Windows.Data.ValueConversionAttribute
Spazio dei nomi: System.Windows.Data
Assembly: PresentationFramework (in PresentationFramework.dll)
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := True)> _ Public NotInheritable Class ValueConversionAttribute _ Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true)] public sealed class ValueConversionAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class, AllowMultiple = true)] public ref class ValueConversionAttribute sealed : public Attribute
[<Sealed>] [<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true)>] type ValueConversionAttribute = class inherit Attribute end
Il tipo ValueConversionAttribute espone i seguenti membri.
| Nome | Descrizione | |
|---|---|---|
|
ValueConversionAttribute | Inizializza una nuova istanza della classe ValueConversionAttribute con il tipo di origine e di destinazione specificato. |
| Nome | Descrizione | |
|---|---|---|
|
ParameterType | Ottiene e imposta il tipo dell'oggetto parametro del convertitore di valore facoltativo. |
|
SourceType | Ottiene il tipo che questo convertitore converte. |
|
TargetType | Ottiene il tipo che questo convertitore converte. |
|
TypeId | Ottiene un identificatore univoco dell'istanza di ValueConversionAttribute. (Esegue l'override di Attribute.TypeId). |
| Nome | Descrizione | |
|---|---|---|
|
Equals | Infrastruttura. Restituisce un valore che indica se l'istanza è uguale a un oggetto specificato. (Ereditato da Attribute) |
|
Finalize | Consente a un oggetto di provare a liberare risorse ed eseguire altre operazioni di pulitura prima che l'oggetto stesso venga recuperato dalla procedura di Garbage Collection. (Ereditato da Object) |
|
GetHashCode | Restituisce il codice hash per l'istanza di ValueConversionAttribute. (Esegue l'override di Attribute.GetHashCode()). |
|
GetType | Ottiene l'oggetto Type dell'istanza corrente. (Ereditato da Object) |
|
IsDefaultAttribute | Quando è sottoposto a override in una classe derivata, indica se il valore di questa istanza è il valore predefinito della classe derivata. (Ereditato da Attribute) |
|
Match | Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato. (Ereditato da Attribute) |
|
MemberwiseClone | Consente di creare una copia dei riferimenti dell'oggetto Object corrente. (Ereditato da Object) |
|
ToString | Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |
| Nome | Descrizione | |
|---|---|---|
|
_Attribute.GetIDsOfNames | Esegue il mapping di un set di nomi a un set corrispondente di ID di invio. (Ereditato da Attribute) |
|
_Attribute.GetTypeInfo | Recupera le informazioni sul tipo relative a un oggetto, che possono essere utilizzate per ottenere informazioni sul tipo relative a un'interfaccia. (Ereditato da Attribute) |
|
_Attribute.GetTypeInfoCount | Recupera il numero delle interfacce di informazioni di tipo fornite da un oggetto (0 o 1). (Ereditato da Attribute) |
|
_Attribute.Invoke | Fornisce l'accesso a proprietà e metodi esposti da un oggetto. (Ereditato da Attribute) |
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.
<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.
<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.
.NET Framework
Supportato in: 4, 3.5, 3.0.NET Framework Client Profile
Supportato in: 4, 3.5 SP1Windows 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.