I want then when user select the date from datepicker on textbox it will show the date like 01-April-2012
Please tell me how it is done??
Thanks in advance
You have to format your textbox. Assuming the text box is already bounded to the DatePicker, it should be something like
<TextBlock Text="{Binding ElementName=DatePicker1, Path=Value, StringFormat={}{0:dd-MMMM-yyyy}}" />
Related
I am having trouble firing a validation once the user enters text to the DatePicker (not when selecting the date through the DatePicker). I want it to fire the validation only when the text is in a format of a date, such as "MM/dd/yyyy", but when using the Text property and UpdateSourceTrigger=PropertyChanged, it will fire validation for every time a text is entered. I also have set my binding SomeDate to a DateTime. Is there a way to do this without changing my binding to a string?
<DatePicker x:Name = "date"
Text = "{Binding SomeDate, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,
TargetNullValue=''}"/>
I also tried using SelectedDate property instead of Text, but this only triggers the validation when you press enter for the text-entered date or when you select the date through the DatePicker UI. I understand why all of this is happening, but cannot think of a solution.
I also tried Text property and UpdateSourceTrigger=Explicit, but looking at the events that can be triggered, they don't seem to be helpful. I tried using PreviewTextInput but that doesn't work either because there is no way to get new text entered. There is also no TextChanged event property for DatePicker.
<DatePicker x:Name = "date" PreviewTextInput = "onInput"
Text = "{Binding SomeDate, Mode=TwoWay,
UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True,
TargetNullValue=''}"/>
Is there a way to do this? Thanks.
I created two DatePickers (dp1 and dp2) in WPF. When I select a date on the dp1, I would like dp2 to update automatically: The date from dp1 should come to dp2. Any ideas on how I should do it?
I prefer a XAML only solution over to codebehind answer.
<StackPanel>
<DatePicker Name="dp1"></DatePicker>
<DatePicker SelectedDate="{Binding ElementName=dp1, Path=SelectedDate}"></DatePicker>
</StackPanel>
I have a datepicker in a Datagrid row. What I want is when a user clicks in the row and the datepicker that the date goes to today's date.
This code below does work if you go straight to the datepicker and then go and pick a date, however the problem is when you click inside the row first then the datepicker goes to 01/01/0001.
I have this XAML but it does not work.
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{x:Static System:DateTime.Now}"></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Any help would be grateful
I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").
The following does not work. I get a short date format (m/d/yyyy).
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
I've considered using a converter, but I prefer a pure XAML approach.
Edit:
For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.
Edit 2:
I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
Anyone know why it doesn't work with Label?
Thanks.
ContentControls have a ContentStringFormat property which overrides the original formatting.
(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)
Your month needs to be in uppercase:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
EDIT:
The Label problem is probably because Label has Content, not Text.
Change the Text="{Binding ...}" to Content="{Binding ...}"
This works great:
<my:DatePicker IsTodayHighlighted="True" Width="200">
</my:DatePicker>
But I want to format the date, something like this:
<my:DatePicker IsTodayHighlighted="True" Width="200" Format="yyyy-mm-dd">
</my:DatePicker>
Anyone know the syntax for this?
Unfortunately the DatePicker control currently does not support free DateTime formats.
If this is something you're interested in seeing up support in future version of DatePicker, please create a codeplex feature request that suggests that.
http://silverlight.codeplex.com/WorkItem/Create.aspx
Just to point out that the new Silverlight Toolkit March 2009 TimePicker & TimeUpDown controls do support a full range of globalization options. One of those include free DateTime formats. So it is just a matter of public interest on whether or not we port that ability back to DatePicker.
Have a look at the format for TimePicker #
http://silverlight.codeplex.com/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%201#TimePicker
In the meanwhile, The best workaround is to either change the local culture or the format on the local culture.
public App()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
or change the format on the local culture.
public App()
{
Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "D/m/yyyy";
You should define a custom control template and edit the textbox of the datepicker control to format the text.
The Silverlight DatePicker has a SelectedDateFormat Property on it, this may be what you are looking for.
You could just hide the controls textbox (with a smaller width), expose you're own (optionally set the IsEnabled to false) and use an Element binding and Converter. If you're using MVVM, then set the DataContext to your ViewModel. I suppose another option would be to overwrite the DataTemplate to not include the text box and do the same idea.
<StackPanel Orientation="Horizontal" Height="22">
<TextBox x:Name="textBox2" Width="106" Text="{Binding ElementName=datePicker2, Path=SelectedDate, Mode=TwoWay, Converter={StaticResource internationalDateTimeFormatConverter}}" />
<controls:DatePicker x:Name="datePicker2" IsTabStop="False" SelectedDate="{Binding TargetDatePicker, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="23" HorizontalAlignment="Left" />
</StackPanel>
UPDATE:
The TwoWay binding from the text box to the the date picker works well, but it doesn't update the ViewModel Property. So I'm going to set the IsEnabled=False and call it good.
I notice this is an answered question.
But I would like to notify about this link to a Silverlight 5 control toolkit I have recently started creating. It contains (among other controls) a DateTimeBox control in which you can handle both date and time within the same control. At this point, it is still under development, but it should be usable for most scenarios.