Wednesday, October 6, 2010

Convert Date and Time to proper format in Silverlight Datagrid or Map Tooltip

When you map Date Field to Silverlight 3 Datagrid Control or Tooltip control the full date and time value will appear. For example like 12/13/2010 12:00:00. Sometimes it is required to display only the Date value or only the time value.

Here are the steps to follow

1. In the .xaml file make sure the application namespace is refered properly like shown in this example.

xmlns:CustomControls="clr-namespace:SilverlightGISApp"

2. In the .xaml file add the converter class as a Grid resource. In this example the class CValueConverter is used.

<Grid.Resources>
<CustomControls:CValueConverter x:Name="MyToolTipConverter"></CustomControls:CValueConverter>

3. In case of datagrid use the following sample

<esriToolkit:FeatureDataGrid Grid.Row="0" Grid.Column="0" x:Name="DataGridControl" Width="210"
Map="{Binding ElementName=MapControl}" AutoGeneratingColumn="DataGridControl_AutoGeneratingColumn"
GraphicsLayer="{Binding ElementName=MapControl, Path=Layers.[5]}" CurrentCellChanged="DataGridControl_CurrentCellChanged" >
</esriToolkit:FeatureDataGrid>

4. In case of Tooltip use the following sample

<StackPanel Orientation="Horizontal">
  <TextBlock x:Name="LabelIncidentDate" Text="Incident Date: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="10" VerticalAlignment="Center"/>
  <TextBlock x:Name="IncidentDate" Text="{Binding Converter={StaticResource MyToolTipConverter}, ConverterParameter=alm_date, Mode=OneWay}" HorizontalAlignment="Left"             VerticalAlignment="Center"  />
</StackPanel>


5. Note in DataGrid the "MyToolTipConverter" resource is not used. This is because it is handled programatically during the "Column Generating Event".

private void DataGridControl_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTextColumn dataGridTextColumn = (DataGridTextColumn)e.Column;
if ((e.Column.Header.ToString().ToUpper() == IncidentDateColName.ToUpper()))
{
CValueConverter oDateConverter = new CValueConverter();
dataGridTextColumn.Binding.ConverterParameter = e.Column.Header.ToString();
dataGridTextColumn.Binding.Converter = oDateConverter;
}
if ((e.Column.Header.ToString().ToUpper() == IncidentTimeColName.ToUpper()))
{
e.Cancel = true;
}
if ((e.Column.Header.ToString().ToUpper() == FireIncidentsUniqueIDFieldName.ToUpper()))
{
e.Cancel = true;
}
}

6. Finally here is the custom class which will do the conversion task. Note down the signature of the “Convert” function. The required values will come in and the necessary logic can be implemented.
namespace SilverlightGISApp
{
    public class CValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is string)
            {
                char[] ToSplitDate = { '/' };
                char[] ToSplitTime = { ':' };
                string[] InStringDate = value.ToString().Split(ToSplitDate);
                string[] InStringTime = value.ToString().Split(ToSplitTime);
                int Month = System.Convert.ToInt16(InStringDate[0]);
                int Day = System.Convert.ToInt16(InStringDate[1]);
                int Year = System.Convert.ToInt16(InStringDate[2].Substring(0, 4));
                int Hour = System.Convert.ToInt16(InStringTime[0].Substring((InStringTime[0].Length - 2), 2));
                int Min = System.Convert.ToInt16(InStringTime[1]);
                int Sec = System.Convert.ToInt16(InStringTime[2].Substring(0, 2));
                string AMorPM = InStringTime[2].Substring((InStringTime[2].Length - 2), 2);
                if ((AMorPM == "PM") && (Hour != 12)) Hour = Hour + 12;
                DateTime date = new DateTime(Year, Month, Day, Hour, Min, Sec);
                string ToGoValue = "";
                if (parameter.ToString().ToUpper() == MainPage.IncidentDateColName.ToUpper()) ToGoValue = date.ToShortDateString();
                if (parameter.ToString().ToUpper() == MainPage.IncidentTimeColName.ToUpper()) ToGoValue = date.ToShortTimeString();
                return ToGoValue;
            }
            else if ((value is System.Collections.Generic.Dictionary<string, object>) && (MainPage.IncidentTypeColName.Trim().ToUpper() != parameter.ToString().Trim().ToUpper()))
            {
                System.Collections.Generic.Dictionary<string, object> theDictionary = value as System.Collections.Generic.Dictionary<string, object>;
                string theDateTimeValue = theDictionary[parameter as string].ToString();

                char[] ToSplitDate = { '/' };
                char[] ToSplitTime = { ':' };
                string[] InStringDate = theDateTimeValue.ToString().Split(ToSplitDate);
                string[] InStringTime = theDateTimeValue.ToString().Split(ToSplitTime);
                int Month = System.Convert.ToInt16(InStringDate[0]);
                int Day = System.Convert.ToInt16(InStringDate[1]);
                int Year = System.Convert.ToInt16(InStringDate[2].Substring(0, 4));
                int Hour = System.Convert.ToInt16(InStringTime[0].Substring((InStringTime[0].Length - 2), 2));
                int Min = System.Convert.ToInt16(InStringTime[1]);
                int Sec = System.Convert.ToInt16(InStringTime[2].Substring(0, 2));
                string AMorPM = InStringTime[2].Substring((InStringTime[2].Length - 2), 2);
                if ((AMorPM == "PM") && (Hour != 12)) Hour = Hour + 12;
                DateTime date = new DateTime(Year, Month, Day, Hour, Min, Sec);
                string ToGoValue = "";
                if (parameter.ToString().ToUpper() == MainPage.IncidentDateColName.ToUpper()) ToGoValue = date.ToShortDateString();
                if (parameter.ToString().ToUpper() == MainPage.IncidentTimeColName.ToUpper()) ToGoValue = date.ToShortTimeString();
                return ToGoValue;
            }
            else if ((value is System.Collections.Generic.Dictionary<string, object>) && (MainPage.IncidentTypeColName.Trim().ToUpper() == parameter.ToString().Trim().ToUpper()))
            {
                System.Collections.Generic.Dictionary<string, object> theDictionary = value as System.Collections.Generic.Dictionary<string, object>;
                string thetypeValue = theDictionary[parameter as string].ToString();

                string ToGoValue = "";
                if (thetypeValue == "100") ToGoValue = "E.M.S";
                if (thetypeValue == "311") ToGoValue = "Fire";
                if (thetypeValue == "324") ToGoValue = "Other";
                if (thetypeValue.Trim() == "") ToGoValue = "Other";
                return ToGoValue;
            }
            else
            {
                string ToGoValue = "";
                return ToGoValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string strValue = value.ToString();
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return value;
        }
    }
}


All Blogs so far ...