Back to Devexpress

ValueConverterAttribute.Converter Property

xpo-devexpress-dot-xpo-dot-valueconverterattribute.md

latest3.2 KB
Original Source

ValueConverterAttribute.Converter Property

Gets the value converter.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public ValueConverter Converter { get; }
vb
Public ReadOnly Property Converter As ValueConverter

Property Value

TypeDescription
ValueConverter

A ValueConverter descendant which represents the value converter.

|

Remarks

If the value converter doesn’t exist, a new instance of the ValueConverterAttribute.ConverterType is returned.

Example

The following code example demonstrates how to store your boolean data in the database using the “T” string for true and “F” for false by defining a new ValueConverter descendant. You may need this converter when using an existing database in which boolean properties are stored such form.

csharp
//...       
[Size(1), ValueConverter(typeof(BooleanToStringValueConverter))]
public bool Answer {
    get { return GetPropertyValue<bool>(nameof(Answer)); }
    set { SetPropertyValue<bool>(nameof(Answer), value); }
}

//...
public class BooleanToStringValueConverter : ValueConverter {
    public override object ConvertFromStorageType(object value) {
        return Convert.ToString(value) == "T";
    }
    public override object ConvertToStorageType(object value) {
        return Convert.ToBoolean(value) ? "T" : "F";
    }
    public override System.Type StorageType {
        get { return typeof(string); }
    }
}
vb
'...       
<Size(1), ValueConverter(GetType(BooleanToStringValueConverter))> _
Public Property Answer() As Boolean
    Get
        Return GetPropertyValue(Of Boolean)(NameOf(Answer))
    End Get
    Set(ByVal value As Boolean)
        SetPropertyValue(Of Boolean)(NameOf(Answer), value)
    End Set
End Property

'...
Public Class BooleanToStringValueConverter
    Inherits ValueConverter
    Public Overrides Function ConvertFromStorageType(ByVal value As Object) As Object
        Return Convert.ToString(value) = "T"
    End Function
    Public Overrides Function ConvertToStorageType(ByVal value As Object) As Object
        Return If(Convert.ToBoolean(value), "T", "F")
    End Function
    Public Overrides ReadOnly Property StorageType() As System.Type
        Get
            Return GetType(String)
        End Get
    End Property
End Class

See Also

ConverterType

ValueConverterAttribute Class

ValueConverterAttribute Members

DevExpress.Xpo Namespace