Data Access has been discontinued. Please refer to this page for more information.

Creating Converters and the Base View Interface

In this step you will create two converters which will be used through out your views and an interface which defines the core functionality all views will expose.

Creating converters for the views

  1. In the Views folder add a Class named NullableBooleanConverter. This class must implement IValueConverter and it will be used to convert null to False for some of the bound controls in your views.

    public class NullableBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            object result = this.NullableBooleanToFalse(value);
            return result;
        }
        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            object result = this.NullableBooleanToFalse(value);
            return result;
        }
        private object NullableBooleanToFalse(object value)
        {
            if (value == null)
            {
                return false;
            }
            else
            {
                return value;
            }
        }
    }
    
    Public Class NullableBooleanConverter
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim result As Object = Me.NullableBooleanToFalse(value)
            Return result
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Dim result As Object = Me.NullableBooleanToFalse(value)
            Return result
        End Function
        Private Function NullableBooleanToFalse(value As Object) As Object
            If IsNothing(value) Then
                Return False
            Else
                Return value
            End If
        End Function
    End Class
    
  2. In the Views folder add a Class named YearConverter. This class must implement IValueConverter and it will be used to convert null values to the current year for some of the bound controls in your views.

    public class YearConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            object result = this.NullYearToCurrentYear(value);
            return result;
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            object result = this.NullYearToCurrentYear(value);
            return result;
        }
        private object NullYearToCurrentYear(object value)
        {
            if (value == null)
            {
                value = DateTime.Now.Year;
            }
            return value;
        }
    }
    
    Public Class YearConverter
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim result As Object = Me.NullYearToCurrentYear(value)
            Return result
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Dim result As Object = Me.NullYearToCurrentYear(value)
            Return result
        End Function
        Private Function NullYearToCurrentYear(value As Object) As Object
            If IsNothing(value) Then
                value = DateTime.Now.Year
            End If
            Return value
        End Function
    End Class
    
  3. In the Views folder add a Class named EmptyStringConverter. This class must implement IValueConverter and it will be used to convert empty string values to null for some of the bound controls in your views.

    public class EmptyStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            object result = this.EmptyStringToNull(value);
            return result;
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            object result = this.EmptyStringToNull(value);
            return result;
        }
        private object EmptyStringToNull(object value)
        {
            string stringValue = (string)value;
            if (string.IsNullOrEmpty(stringValue))
            {
                return null;
            }
            return value;
        }
    }
    
    Public Class EmptyStringConverter
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim result As Object = Me.EmptyStringToNull(value)
            Return result
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object,
                     culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Dim result As Object = Me.EmptyStringToNull(value)
            Return result
        End Function
        Private Function EmptyStringToNull(value As Object) As Object
            Dim stringValue As String = CType(value, String)
            If String.IsNullOrEmpty(value) Then
                Return Nothing
            End If
            Return value
        End Function
    End Class
    

Defining the base view interface

  1. In the ViewInterfaces folder add a new Interface named IView. This interface will define the base functionality of all views - they can be closed through the Close() method and have a DataContext which can be set or retrieved.

    public interface IView
    {
        object DataContext { get; set; }
        void Close();
    }
    
    Public Interface IView
        Property DataContext As Object
        Sub Close()
    End Interface
    

Next step: Setting up the Main View