Assembly: Dapfor.Wpf (in Dapfor.Wpf.dll) Version: 4.1.0.26317 (4.1.0.26317)
Syntax
C# |
---|
public IValueConverter ValueConverter { get; set; } |
Visual Basic |
---|
Public Property ValueConverter As IValueConverter Get Set |
Visual C++ |
---|
public: property IValueConverter^ ValueConverter { IValueConverter^ get (); void set (IValueConverter^ value); } |
F# |
---|
member ValueConverter : IValueConverter with get, set |
Property Value
Type: IValueConverterThe IValueConverter.
Remarks
MVVM data model implies separation of data from presentation with data binding. In this model data can be presented in different ways in different controls. Data binding enables binding business logic with graphical controls on data layer. When controls are initialized, the data layer provides values that are later transformed to required types and displayed on the presentation layer. User may change values with corresponding controls (combo box, text box, etc). New value entered by user is transformed to the required type and transmitted to the data layer via data binding. Type conversion is done via classes that implement IValueConverter interface.
An example of implementing a data converter that transforms business logic values to strings:
Copy | |
---|---|
public class IntToStringConverter : IValueConverter { // Converts int-type value to string public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is int) { return string.Format(culture, "{0}", value); } return null; } // Parses string to int-type value public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string s = value as string; if (!string.IsNullOrEmpty(s)) { return int.Parse(s, culture); } return Binding.DoNothing; } } |
Programmer has the following ways to set type converters.
- Declarative method with attributes.
- Setting type converters in grid columns.
Copy | |
---|---|
class MyDataObject { //Specific converter to present double values as strings with 2 digits after dot [DoubleConverter(2)] public double SomeDoubleValue { get; set; } //Generic converter to format value with the specified format [Converter("yyyy-MM-dd HH:mm:ss")] public DateTime SomeDateValue { get; set; } } |
If converters provided by Dapfor's framework are not sufficient, programmers may create their own converters and attributes to use in declarative formatting. An example of implementing an attribute for declarative setting of the above shown IntToStringConverter:
Copy | |
---|---|
public class IntToStringConverterAttribute : ConverterBaseAttribute { private readonly IntToStringConverter _converter; public IntToStringConverterAttribute() { _converter = new IntToStringConverter(); } public override IValueConverter Converter { get { return _converter; } } } |
As we can see, IntToStringConverterAttribute create an instance of IntToStringConverter and returns it in a virtual property. IntToStringConverterAttribute class can also be enhanced by builders with parameters for sending them to IntToStringConverter during initialization. An example of using the attribute created above is shown below:
Copy | |
---|---|
class MyDataObject { //... [IntToStringConverter] public int SomeIntValue { get; set; } } |
Another way to use converters is to set them directly in grid columns. This method is used in cases when usage of declarative formatting is not desirable or when it is necessary to display data in different grids differently. The below example demonstrates usage of converters in grid columns.
Copy | |
---|---|
GridControl grid = ...; grid.Headers[0]["SomeIntValue"].ValueConverter = new IntToStringConverter(); |