Variable Date Format for WPF DataGrid in C# -
i have datetime
field in list<>
used itemsource
in wpf datagrid
format differently depending on value.
if datetime
value within year of datetime.now
, want use 'mmm dd hh:mm'
formatting, if beyond year of datetime.now
, want use 'mmm dd yyyy'
formatting, , if datetime.minvalue
, wish cell in data grid blank.
this code have currenly shows dates 'mmm dd hh:mm'
format.
is possible define variable formatting directly in xaml?
if not, how can patch formatting programatically in datagrid_loaded event?
public class deadlineitem { public string name { get; set; } public datetime deadline {get; set; } public deadlineitem(string aname, datetime adeadline) { this.name = aname; this.deadline = adeadline; } } private void datagrid_loaded(object sender, routedeventargs e) { var items = new list<deadlineitem>(); items = new list<deadlineitem>(); items.add(new deadlineitem("distant deadline", datetime.now.adddays(500))); items.add(new deadlineitem("near deadline", datetime.now.adddays(1))); items.add(new deadlineitem("no deadline", datetime.minvalue)); // ... assign itemssource of datagrid. var grid = sender datagrid; grid.itemssource = items; }
and xaml is:
<datagrid autogeneratecolumns="false" horizontalalignment="left" margin="0,0,0,0" verticalalignment="top" loaded="datagrid_loaded"> <datagrid.columns> <datagridtextcolumn header="name" binding="{binding name}" /> <datagridtextcolumn header="deadline" binding="{binding deadline, stringformat=\{0:mmm dd hh:mm\}}" /> </datagrid.columns> </datagrid>
as eugene mentioned, you'll want use converter. first, you'll want add class implements ivalueconverter
:
public class datetimeformatconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { var datevalue = value datetime?; if (datevalue != null) { var diff = datevalue - datetime.now; // greater 1 year, format mm/dd/yyyy if (diff > new timespan(365, 0, 0, 0)) { return datevalue.value.tostring("mmm dd yyyy"); } else if (datevalue == datetime.minvalue) { return ""; } else { return datevalue.value.tostring("mmm dd hh:mm"); } } return null; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }
now, need reference converter in xaml file. like:
<window.resources> <testwpf:datetimeformatconverter x:key="datetimeformatconverter"></testwpf:datetimeformatconverter> </window.resources>
lastly, you'll change binding use converter:
<datagrid autogeneratecolumns="false" horizontalalignment="left" margin="0,0,0,0" verticalalignment="top" loaded="datagrid_loaded"> <datagrid.columns> <datagridtextcolumn header="name" binding="{binding name}" /> <datagridtextcolumn header="deadline" binding="{binding deadline, converter={staticresource datetimeformatconverter}}" /> </datagrid.columns> </datagrid>
you don't need worry sorting, datagrid sort on underlying date, not formatted string.
Comments
Post a Comment