c# - Binding string property to textblock and apply custom date format -
i have textblock in wpf application binding string property displays date , time. there way apply stringformat on string property format date content. tried follows dosent work. please help.
in model property
public string alerttimestamp { get; set; }
in view trying
<textblock grid.row="0" grid.column="2" horizontalalignment="right" margin="25,5,0,0" text="{binding alerttimestamp, stringformat=0:dd mmm yyyy hh:mm:ss tt}"></textblock>
the output still 7/25/2016 12:20:23 pm
you need add ivalueconverter change string
object datetime
. this.
public class stringtodatevalueconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { return datetime.parse(value.tostring()); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
of course, want implement form of validation logic, i've excluded sake of simplicity.
the markup...
<window x:class="wpfapplication4.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapplication4" mc:ignorable="d" title="mainwindow" height="350" width="525"> <window.datacontext> <local:viewmodel /> </window.datacontext> <window.resources> <local:stringtodatevalueconverter x:key="converter" /> </window.resources> <grid> <textblock grid.row="0" grid.column="2" horizontalalignment="right" margin="25,5,0,0" text="{binding alerttimestamp, converter={staticresource converter}, stringformat=dd mmm yyyy hh:mm:ss tt}"></textblock> </grid> </window>
Comments
Post a Comment