WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -
i have problem wpf binding. want bind list of months itemscontrol shows calendar control each month. each rendered calendar shows datetime.now,not bound datetimes. know why happening?
this have far:
the mainwindow.xaml
<window x:class="calendarlisttest.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <itemscontrol x:name="calendarlist"> <itemscontrol.itemtemplate> <datatemplate> <calendar displaydate="{binding currentdate}" /> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid>
** place collection assigned itemssource**
private void window_loaded( object sender, routedeventargs e ) { calendarlist list = new calendarlist( ); list.add( new calendarmonth( ) { currentdate = datetime.parse( "1.1.1979" ) } ); list.add( new calendarmonth( ) { currentdate = datetime.parse( "1.2.1979" ) } ); list.add( new calendarmonth( ) { currentdate = datetime.parse( "1.3.1979" ) } ); calendarlist.itemssource = list; }
the calendarmonth viewmodel:
public class calendarmonth { private datetime _currentdate; public datetime currentdate { { return _currentdate; } set { _currentdate = value; } } }
and collection bind itemscontrol:
public class calendarlist : observablecollection<calendarmonth> { }
now, result:
why happening?
edit: when providing <calendar displaydate="{binding currentdate, mode=oneway}" />
binding works.
the issue appears how calendar initializes displaydate property. this:
public calendar() { // ... base.setcurrentvalueinternal(displaydateproperty, datetime.today); }
it appears though displaydate being initialized before binding established, still pushed binding source if set after. bug.
you can work around using like:
public class mycalendar : calendar { public mycalendar() { this.clearvalue(displaydateproperty); } }
or establish binding @ later time (i.e. when calendar loaded) using event handler or attached behavior.
Comments
Post a Comment