Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

BindingBase.StringFormat, standard date and time formats, customized using Regional Settings in Control Panel.

$
0
0

I am formatting dates using the StringFormat property of a data binding (System.Windows.Data.Binding). When I select a different region/language using the regional settings of the Windows control panel, the data binding formats dates correctly, according to the newly selected region/language. However, when I select one of the alternate short date formats, the data binding formats dates according to the original short date format for the currently selected region/language.

The confusing thing is that the CurrentCulture.DateTimeFormat.ShortDatePattern matches the alternate value specified in the regional settings of the control panel.

I've written a small application to demonstrate this problem. The following are some sample outputs from the application:

When I select English (Canada) as the format for date/time strings from the control panel, the output from my test application is as expected:

When I select an alternate short date format (yyyy-MM-dd), that format is active in CurrentCulture, however the original short date format for en-CA is used to format the date strings:

Similar results occur when selecting languages other than English.

The following is the XAML for my test application:

<Window x:Class="WpfDateTimeFormatTestApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Date Time Format Test" SizeToContent="WidthAndHeight"><Grid Margin="20"><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><Label>{Binding Path=Language} =</Label><Label Grid.Column="1"><TextBlock Text="{Binding Path=Language}"/></Label><Label Grid.Row="1">{Binding Path=CurrentCulture} =</Label><Label Grid.Column="1" Grid.Row="1"><TextBlock Text="{Binding Path=CurrentCulture}"/></Label><Label Grid.Row="2">{Binding Path=CurrentCulture.DateTimeFormat.ShortDatePattern} =</Label><Label Grid.Column="1" Grid.Row="2"><TextBlock Text="{Binding Path=CurrentCulture.DateTimeFormat.ShortDatePattern}"/></Label><Label Grid.Row="3">{Binding Path=CurrentUICulture} =</Label><Label Grid.Column="1" Grid.Row="3"><TextBlock Text="{Binding Path=CurrentUICulture}"/></Label><Label Grid.Row="4">{Binding Path=CurrentUICulture.DateTimeFormat.ShortDatePattern} =</Label><Label Grid.Column="1" Grid.Row="4"><TextBlock Text="{Binding Path=CurrentUICulture.DateTimeFormat.ShortDatePattern}"/></Label><Label Grid.Row="5">{Binding Path=Now} = </Label><Label Grid.Column="1" Grid.Row="5"><TextBlock Text="{Binding Path=Now}"/></Label><Label Grid.Row="6">{Binding Path=Now, StringFormat=d} =</Label><Label Grid.Column="1" Grid.Row="6"><TextBlock Text="{Binding Path=Now, StringFormat=d}"/></Label><Label Grid.Row="7">{Binding Path=Now, StringFormat=D} =</Label><Label Grid.Column="1" Grid.Row="7"><TextBlock Text="{Binding Path=Now, StringFormat=D}"/></Label></Grid></Window>

The following is the code for my test application:

using System;
using System.Globalization;
using System.Windows;

namespace WpfDateTimeFormatTestApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Language = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag);

            InitializeComponent();

            DataContext = this;
        }

        public DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        public CultureInfo CurrentCulture
        {
            get
            {
                return CultureInfo.CurrentCulture;
            }
        }

        public CultureInfo CurrentUICulture
        {
            get
            {
                return CultureInfo.CurrentUICulture;
            }
        }
    }
}

The target platform of the test application is .Net 3.5.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>