wpf - DatePicker SelectedDate string format -
wpf - DatePicker SelectedDate string format -
i have derived datepicker class back upwards additional datemode property allow user utilize command per datemode (day, month, year). if datemode set year, command not able farther drill downwards see months of year , farther days of month. command working good, 1 problem. though have applied string formatting on 'part_textbox' command datepicker template, alter formatting based on datemode, datepicker command loses focus formatting lost. next derived datepicker command code:
public class mydatepicker : datepicker { public string datemode { { homecoming (string)getvalue(datemodeproperty); } set { setvalue(datemodeproperty, value); } } // using dependencyproperty backing store datemode. enables animation, styling, binding, etc... public static readonly dependencyproperty datemodeproperty = dependencyproperty.register("datemode", typeof(string), typeof(mydatepicker), new uipropertymetadata("day")); protected override void oncalendaropened(routedeventargs e) { var popup = this.template.findname("part_popup", this) popup; var tb = this.template.findname("part_textbox", this) textbox; if (popup != null && popup.child system.windows.controls.calendar) { if (datemode == "year") ((system.windows.controls.calendar)popup.child).displaymode = calendarmode.decade; else if (datemode == "month") ((system.windows.controls.calendar)popup.child).displaymode = calendarmode.year; else if (datemode == "day") ((system.windows.controls.calendar)popup.child).displaymode = calendarmode.month; } ((system.windows.controls.calendar)popup.child).displaymodechanged += new eventhandler<calendarmodechangedeventargs>(datepickerco_displaymodechanged); } protected override void oncalendarclosed(routedeventargs e) { base.oncalendarclosed(e); isdropdownopen = false; var popup = this.template.findname("part_popup", this) popup; ((system.windows.controls.calendar)popup.child).displaymodechanged -= new eventhandler<calendarmodechangedeventargs>(datepickerco_displaymodechanged); } private void datepickerco_displaymodechanged(object sender, calendarmodechangedeventargs e) { var popup = this.template.findname("part_popup", this) popup; var tb = this.template.findname("part_textbox", this) textbox; if (popup != null && popup.child system.windows.controls.calendar) { var _calendar = popup.child system.windows.controls.calendar; if (datemode == "month" && _calendar.displaymode == calendarmode.month) { if (isdropdownopen) { this.selecteddate = _calendar.displaydate; this.isdropdownopen = false; _calendar.displaymode = calendarmode.year; } tb.text = this.selecteddate.value.tostring("mmm yyyy"); } else if (datemode == "year" && _calendar.displaymode == calendarmode.year) { if (isdropdownopen) { this.selecteddate = _calendar.displaydate; this.isdropdownopen = false; _calendar.displaymode = calendarmode.decade; } tb.text = this.selecteddate.value.tostring("yyyy"); } } } }
i think you're getting interference base of operations class. i'm not sure of exact setup think part_textbox
has binding that's set datepicker
, updating on lostfocus , looping clear out text
value had set directly. might seek using bindingoperations.getbinding
access binding on text
, modify it's stringformat instead of setting text
directly.
wpf datepicker string-formatting
Comments
Post a Comment