I have my table DataType is set to Date in SQL Server but is showing the DateTime in my WPF textbox and I'm not sure how I can format the textbox or SQL Server to only show Date.
I tried using DatePicker instead of a TextBox but I need it to be ReadOnly and the DatePicker is grayed out when it is set to NOT ENABLED and there is no option for ReadOnly.
How can I only display the short date in my TextBox?
I think this code should solve your problem:
<TextBox Text="{Binding PropertyPath, StringFormat=d}" />
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 have couple of tables in database with Datatype DateTime and default value getdate() in sql server. while I am binding this to textbox it is displaying 1/1/0001 as default. I am using Silverlight Ria services.
<TextBox Text="{Binding Mode=TwoWay, Path=LST_UPDT_TS, StringFormat=\{0:D\}}"/>
Thank you for helping and for ur time..
The textbox is displaying the default value for DateTime, which means two things:
The Binding worked (else the TextBox would be empty).
The property DateTime LST_UPDT_TS... has not been set
You should check that the property is being set properly, and that you are binding to the correct instance.
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}}" />
I have a combo box that I want to bind to a list of datetime objects, but I want to show the datetime objects in short time format. I'm pretty sure I need to use some form of data template for this, but I can't figure out how to bind to the datetime object's ToShortTime method within the data template.
Can someone point me in the right direction?
Assuming you're using .NET 3.0 or 3.5 with SP1, you can simply use the StringFormat to specify the format, for example:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{}{0:t}'}" />
Will show the current date time with short time format ('t' standard date time format modifier, exactly the same as calling DateTime.Now.ToString("t")).
Edit: If you're already in a data template having a DateTime as the DataContext, just use:
<TextBlock Text="{Binding StringFormat='{}{0:t}'}" />
You could set the Converter property on the binding. Implement IValueConverter to make the change to a string. The MSDN docs for IValueConverter actually use this as an example.
There is a property named:
Combobox.ItemStringFormat
here you can provide the Stringformat as usual. No need to Datatemplate the items for this purpose.
<ComboBox ItemsSource="{Binding MyDates}"
ItemStringFormat="yyyy-MM-dd" />
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.