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 is provided below.
C# |
---|
internal class IntToStringConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is int) { return string.Format(culture, "{0}", value); } return null; } 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; } #endregion }
|
IValueConverter may actually convert business logic to any data types. We will be most interested in conversion to String and back. The grid uses this conversion method to display rows in grid cells.
Programmer has the following ways to set type converters.
- Declarative method with attributes.
- Setting type converters in grid columns.
Declarative method is often use for unified data presentation in the application. Converters are set with attributes for data object properties. There is a set of predefined attributes that can be used to set the required presentation.
C# |
---|
class MyDataObject { [DoubleConverter(2)] public double SomeDoubleValue { get; set; }
[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:
C# |
---|
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:
C# |
---|
class MyDataObject { [IntToStringConverter] public int SomeIntValue { get; set; } } |
Declarative formatting has one important feature – if a grid displays data of different types in the same column, and if no converters are used in this column, data in grid cells may be displayed differently depending on converters set in data object properties.
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.
C# |
---|
GridControl grid = ...; grid.Headers[0]["SomeDoubleValue"].ValueConverter = new DoubleConverter(2); |