c# - Xaml binding to attached property referencing another property -
re-worked per peter’s suggestion.
i have xceed datetimepicker. accepts datetime? value display , bind to.
i have special date – 1900-01-01 00:00:00
if pass date datetimepicker, treated if passed in null – ie, date not shown.
when retrieve value, if date hasn’t been set ie null, return same special date – 1900-01-01 00:00:00.
for other dates, whatever goes in goes out.
in code behind (pseudo code):
datetime specialdate = new datetime(1900,1,1,0,0,0) datetime testdate = new datetime(2016, 2, 8, 10, 0, 0); datetimepicker dtp = new datetimepicker(); // setting value. if (testdate != specialdate) dtp.value = testdate; // getting value if (dtp.value == null) return specialdate; else return (datetime)dtp.value;
can achieve same kind of thing using xaml , binding ?
use simple converter convert , convertback values:
public class datetimetosentinaldateconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (system.convert.todatetime(value).equals(new datetime(1900, 1, 1, 0, 0, 0))) { return null; } else { return value; } } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value == null) { return new datetime(1900, 1, 1, 0, 0, 0); } else { return value; } } }
and in xaml binding be:
value="{binding path=date , converter={staticresource datetimetosentinaldateconverter}}"
Comments
Post a Comment