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.
Related
What is the best way to have a WPF DatePicker show a predefined DateTime (e.g. DateTime.Today) while still maintaining binding?
SelectedDate="{x:Static sys:DateTime.Today}"
and
Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty, Mode=TwoWay}"
don't play well together. When a UserControl is loaded, I'd like it to display today's date, until a row is selected. Is there a way to make them get along?
Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty,
Mode=TwoWay, FallbackValue={x:Static sys:DateTime.Today}}"
Use FallbackValue to set a value when it cannot be retrieved from the binding.
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 ...}"
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" />
My DataTemplate for my ListBox has a TextBlock in it. If I click on the TextBlock, I want it to change to a TextBox so I can edit it. Is there a good way to do this?
You may get some ideas here: Tim heuer's editable ListBox
You should be able to use two-way binding of the text. Then, any edits to the Text dependency property for the item should be reflected back to the original data object.
In your DataTemplate, you probably have something like
<TextBox Text="{Binding}" />
Could you try making this
<TextBox Text="{Binding Mode=TwoWay}" />
Unfortunately I am on a machine without the Silverlight SDK on it at the moment, so I can't verify if my syntax is correct on the binding.
I'm seeing some invalid behavior from XAML documents when the CurrentCulture is changed. When I have some elements like this in a Window:
<Window x:Class="WpfLocalizationLocBaml.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
x:Name="wndTest"
Title="Test" Height="300" Width="300">
<StackPanel>
<TextBlock x:Name="lblCultureName"
Text="{Binding Source={x:Static glob:CultureInfo.CurrentCulture},
Path=DisplayName}" />
<TextBlock x:Name="lblLocaleDateValue"
Text="{Binding ElementName=wndTest, Path=TestDate}"/>
<TextBlock x:Name="lblLocaleNumberValue"
Text="{Binding ElementName=wndTest,Path=NumberValue,StringFormat=c}" />
</StackPanel>
</Window>
as well as a MessageBox.Show( NumberValue.ToString("c") ); when the form starts I'm seeing different results.
If I run the form with the default language all is well obviously. However, if I change the culture in code or at startup the bindings to the date and number values still show en-US formatting. The MessageBox.Show() value displayed appropriately reflects the current culture.
Question: Does WPF not respect CurrentCulture in bindings? And if so what exactly determines the culture that is used for the bindings. It's clearly en-US in my case, but regardless what I set in my project as the default language it always binds in en-US.
Any ideas appreciated...
It turns out that WPF does not respect the CurrentCulture by default in bindings, and instead defaults to xml:Lang setting defined in the XAML document or en-US if not provided. This is rather lame behavior - not sure why you would NOT have automatic culture formatting applied as every other UI technology, but...
Luckily there's an easy workaround that can be applied in the document's constructor or a Window/UserControl base class:
// MAKE SURE you set the language of the page explicitly or else
// all number and date formatting occurs using
this.Language = XmlLanguage.GetLanguage(
CultureInfo.CurrentCulture.IetfLanguageTag);
There's more information available in this blog post:
http://www.west-wind.com/weblog/posts/796725.aspx
It's also worth pointing out that the same thing happens in Silverlight, with the same solution except for swapping IetfLanguageTag for Name.