Format Nullable DataTime in a WPF DataGrid

In a WPF application, i display data in a datagrid. In this datagrid there is some DateTime value.

Where DateTime are not getting set, it displays 01/01/0001, it's ok because DateTime are not nullable.

In this case, i would like to display " - " .

I think i need to use a converter.

I tried this :

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Globalization; using System.Windows.Data;

namespace Phoenix_CRM
{
    public class Converter
    {
    }

    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "--/--/----";

            DateTime returnVal;

            if (DateTime.TryParse(value.ToString(), out returnVal))
            {
                if (returnVal != DateTime.MinValue)
                    return returnVal;
                else
                    return "--/--/----";
            }
            else
                return "--/--/----";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return DateTime.MinValue;

            DateTime val;
            if (value.ToString() == "--/--/----")
                return DateTime.MinValue;

            if (DateTime.TryParse(value.ToString(), out val))
                return val;
            else
                return DateTime.MinValue;
        }

    }

}

I added this on my XAML :

xmlns:converter="clr-namespace:myNameSpace" 

On after :

<DataGrid Grid.Column="0" Grid.Row="2">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{ Binding Path= dDate,StringFormat={}{0:dd/MM/yyyy HH:mm},Converter={converter:Converter.DateConverter}}" Header="  Date  " Width="Auto" IsReadOnly="True"  />
    </DataGrid.Columns>
</DataGrid>

My code don't compile because of the

Converter={converter:Converter.DateConverter}}

Anyone could help me please ?

Answers


To use the converter you have to define it as a resource and use that resource:

<Window.Resources>
    <converter:DateConverter x:Key="DateConverter" />
</Window.Resources>

Usage:

Converter={StaticResource DateConverter}

See this tutorial for more info.


Need Your Help

No error with this collection declared with generics?

java collections

I have below code snippet and this works fine. Shouldn't it throw compile time error because I have defined c as ArrayList which will contain String object but I am adding Integer object. So why it...

Apache HTTPD VirtualHost with Proxy to Tomcat and htaccess file not being checked

apache tomcat virtualhost mod-proxy

I have Apache fronting Tomcat and using *mod_proxy_ajp* to pass requests to Tomcat using the following in the VirtualHost config:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.