Skip to main content

Microsoft Silverlight

Answered Question Date Picker default date issueRSS Feed

(0)

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Date Picker default date issue

Hi,

I've created a dataform with a couple of datepicker controls for the date fields. I have set this up to be an add new record form so the user can add in details and save a record. But when i run the application and open the page (as a silverlight child window), the default values for the datepicker is 01/01/0001. Also, when i select the calendar icon, i get the calendar starting from Jan 0001.

I have tried to set the IsTodayHighlighted property to true & i've tried to set a value for the DiplayDateStart but i still get the default value stated above. The code for the control is as follows:

<dataform:DataField Label="Episode Start Date" >

<controls:DatePicker Language="en-GB" IsTodayHighlighted="True" DisplayDateStart="01/01/2000"

x:Name="EpisodeStartDate" FirstDayOfWeek="Monday" Padding="0"

DisplayDate="{Binding EpisodeStartDate, Converter={StaticResource TimeFixer}}"

SelectedDate="{Binding EpisodeStartDate,Mode=TwoWay, Converter={StaticResource TimeFixer}}"/>

</dataform:DataField>

 

The timefixer resource simply converts the date to UK format & to short format. This works and so does the record save when i add in a more recent date value (save falls if i leave the default date values in).

How can i set this so the default value is a more recent date? Also, how can i restrict the date range available when you select the calendar icon?

Shuja Ahmad

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Date Picker default date issue

Have you tried setting the DisplayDateStart and DisplayDateEnd range?

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Date Picker default date issue

Yep, tried that buut still no luck. i used the following values:

DisplayDateStart="01/01/2000" DisplayDateEnd="11/03/2009"

Shuja Ahmad

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Re: Date Picker default date issue

In your Constructor, try:

DatePicker dp = this.FindName("EpisodeStartDate") as DatePicker;

dp.DisplayDate = DateTime.Now;

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Re: Date Picker default date issue

Hi Ardman,

That sounded like a good solution to me but when i implemented it the dp returned null so brought an error up. I'm assuming that the findname function isnt finding the EpisodeStartDate control. This is what i did:

public AddNewRec()

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

DatePicker dp = (DatePicker)this.FindName("EpisodeStartDate");

dp.DisplayDate = DateTime.Now;

}

Shuja Ahmad

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Re: Re: Date Picker default date issue

If you add a ContentLoaded event handler, then move the DatePicker into there.  This should then be available for the code to pick up the details.

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Re: Re: Date Picker default date issue

Hi Ardman, I know what you mean by "add in a ContentLoaded handler" but i don't know the exact syntax to set it up.

As you might well have gathered, i'm a complete novice at C# & Silverlight development so i struggle with the simplelest of tasks. My apologies for being so poor at this but could you show me how to set up the event handler & where i need to place it?

I don't expect others to do everything for me and i did have a go myself but my code doesn't seem to work:

namespace SusExportLog.Views

{

public partial class AddNewRec : ChildWindow

{

public SUS_ExportLog newsuslog { get; set; }public AddNewRec()

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

}

private void AddNewRec_ContentLoaded(object sender, RoutedEventArgs e)

{

DatePicker dp = this.FindName("EpisodeStartDate") as DatePicker;dp.DisplayDate = DateTime.Now;

}

}

Shuja Ahmad

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Re: Re: Re: Date Picker default date issue

You just need to add in your Constructor:

this.ContentLoaded += new EventHandler(this.AddNewRec_ContentLoaded);

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Re: Re: Re: Date Picker default date issue

Sorry mate, i'm still having no luck with this date picker default date selection. I tried your code but i get an error stating: Error 2 No overload for 'AddNewRec_ContentLoaded' matches delegate 'System.EventHandler' C:\Documents and Settings\ahmadshu\My Documents\Visual Studio 2008\Projects\SusExportLog\SusExportLog\Views\AddNewRec.xaml.cs 30 35 SusExportLog

The code i used is as follows (i also tried this._contentLoaded = ... but got the same error):

public AddNewRec()

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

//this.ContentLoaded += new EventHandler(this.AddNewRec_ContentLoaded);

this.ContentLoaded += new EventHandler(this.AddNewRec_ContentLoaded);

 

 

}

void AddNewRec_ContentLoaded(object sender, RoutedEventArgs e)

{

DatePicker dp = this.FindName("EpisodeStartDate") as DatePicker;

dp.DisplayDate = DateTime.Now;

}

Shuja Ahmad

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Re: Re: Re: Date Picker default date issue

ok, i tried to look the issue up and changed my code around a bit but now i get a different error:

Error 1 Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<System.Windows.Controls.DataFormContentLoadEventArgs>' C:\Documents and Settings\ahmadshu\My Documents\Visual Studio 2008\Projects\SusExportLog\SusExportLog\Views\AddNewRec.xaml.cs 29 46 SusExportLog

The new code is as follows:

public AddNewRec()

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

SusDetailsForm2.ContentLoaded +=
new EventHandler(SusDetailsForm2_ContentLoaded);

//this.ContentLoaded += new EventHandler(this.AddNewRec_ContentLoaded);

}

protected void SusDetailsForm2_ContentLoaded(object sender, EventArgs e)

{

DatePicker dp = this.FindName("EpisodeStartDate") as DatePicker;dp.DisplayDate = DateTime.Now;

}

 

Any ideas on why it is doing that?

Regards, Shuja 

 

Shuja Ahmad

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Re: Re: Re: Re: Date Picker default date issue

In your SusDetailsForm2_ContentLoaded change EventArgs to

DataFormContentLoadEventArgs

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Re: Re: Re: Re: Date Picker default date issue

Ok, i've managed to get the contentLoaded event setup BUT the findname function is still not finding the date picker control. It returns a null value. My control is in a child window, within a grid, within a dataform (called SusDetailsForm2) but the code is still not finding it. Any ideas on how to find the date picker control? Here is my code:

public AddNewRec()

{

try

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

SusDetailsForm2.ContentLoaded += new EventHandler<DataFormContentLoadEventArgs>(SusDetailsForm2_ContentLoaded);

 

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

protected void SusDetailsForm2_ContentLoaded(object sender, DataFormContentLoadEventArgs e)

{

try

{

DatePicker dp = SusDetailsForm2.FindName("EpisodeStartDate") as DatePicker;

dp.DisplayDate = DateTime.Now;

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

Shuja Ahmad

ColinBlair
ColinBlair

Contributor

Contributor

6731 points

1,318 Posts

Re: Date Picker default date issue

The SelectedDate is bound to the EpisodeStartDate. The DatePicker is simply displaying the value that it is bound to. The correct place to default the EpisodeStartDate would be in the entity itself.

-Colin Blair

http://www.RiaServicesBlog.net : The Elephant Guide to RIA Services
SLColinBlair on Twitter

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Date Picker default date issue

Thanks ColinBlair, That makes sense. I have set the default dates for my required fields winthin the entity and this works fine.

BUT I have a date field which is not required by default and so i need to set the DisplayDateStart property to a recent date. I cannot do this in the xaml and when i try to find the date picker control in the code by name it just simply does not find it.

How can i set the DisplayDateStart to a date value in the code? My code now is as follows and i want to set the DateSentToSus date picker control display date start property to the more recent value (instead of the default 01 Jan 0001):

public AddNewRec()

{

try

{

InitializeComponent();

newsuslog =
new SUS_ExportLog();

SusDetailsForm2.CurrentItem = newsuslog;

SusDetailsForm2.BeginEdit();

newsuslog.EpisodeStartDate =
DateTime.Now;

newsuslog.EpisodeEndDate = DateTime.Now;

//newsuslog.DateSentToSUS = DateTime.Now;

SusDetailsForm2.ContentLoaded += new EventHandler<DataFormContentLoadEventArgs>(SusDetailsForm2_ContentLoaded);

 

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

protected void SusDetailsForm2_ContentLoaded(object sender, DataFormContentLoadEventArgs e)

{

try

{

DatePicker dp = SusDetailsForm2.FindName("DateSentToSUS") as DatePicker;

dp.DisplayDateStart = DateTime.Now;

//dp.DisplayDate = DateTime.Now;

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

Shuja Ahmad

ColinBlair
ColinBlair

Contributor

Contributor

6731 points

1,318 Posts

Re: Date Picker default date issue

http://forums.silverlight.net/forums/p/132085/295080.aspx

-Colin Blair

http://www.RiaServicesBlog.net : The Elephant Guide to RIA Services
SLColinBlair on Twitter

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Date Picker default date issue

I don't want a default date selected in the DateSentToSus field. This is meant to be blank until the users have an actual date confirmed. What i needed was the range that the display dates showed to be a more recent date than 01 jan 0001.

I.e. if i create a new record the DateSentToSus can be blank & added later in the edit section. BUT i can create a record which has a DateSentToSus date value confirmed so i can insert that date but when i click o the date picker control i hav to start from 01 jan 0001 when i want the calendar to display this month's date's.

The code in your link seems to be a default display date setting. I don't want a default value, just to have a specified date range in the date picker selection.

Any ideas? 

Shuja Ahmad

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Re: Date Picker default date issue

If i can find the date picker control through code then i would be able to set the date diplay range values but the .FindName funtion cannot seem to pick up the date picker control. I decided to test this and see what ic can pick up and it turn out that if i give the dataform's datatemplate a name (x:name) then the findname will find this. If i tried to find the cancel button by name, the findname will find that too.

BUT my date picker control is within the data template, within the Edit template, within a root stack panel & then within another stack panel  whcih splits the dat into 2 columns. so it is nested within a few things.

But if i try to name any of the stack panels, the findname DOES NOT find these and the control is within the data template within 2 stack panels. I'm assuming that this is the reason i can't find the control. I tried finding the datatemplate & then finding the control through this but the datatemplate does not have a FindName funstion under it.

Can anyone help me find the date picker control through code?

Regards, Shuja

Shuja Ahmad

dabooj80
dabooj80

Member

Member

9 points

81 Posts

Answered Question

Re: Date Picker default date issue

Right, i have managed to stimble on to some sort of solution. I had defined a Binding for the DispayDate as well as the SelectedItem and this was causing the date picker to default the range to start from year 0001. However, when i removed the displaydate from the combox xaml, the date picker calendar showed the current month & today's date was highlighted.

I also removed the displaydate from my editting form and this worked too. When there is an existing value in the date field it displays correctly and when you select the date picker calendar it shows the current month. When there is no date specified then the date picker calendar still displays the current month.

This leads me to ask what is the DisplayDate property used for & what's its point? It seems the selecteditem property does the bindings and the calendar range is defaulted by setting the IsTodayHighlighted so what purpose does the DisplayDate property serve?    

Shuja Ahmad
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities