locked
Silverlight & CurrentCulture RRS feed

  • Question

  •  

    I have a user control like this:

    <UserControl x:Class="SilverlightApplication6.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Width="400" Height="300">

        <Grid x:Name="LayoutRoot" Background="White">

            <TextBlock x:Name="lblDate" Text="{Binding date}"></TextBlock>

        </Grid>

    </UserControl>

    With a codebehind file like this:

    public partial class MainPage : UserControl

        {

            public MainPage()

            {

                InitializeComponent();

                Loaded += new RoutedEventHandler(MainPage_Loaded);

            }

     

            void MainPage_Loaded(object sender, RoutedEventArgs e)

            {

                lblDate.DataContext = new Test { date = DateTime.Now };

            }

        }

     

        public class Test

        {

            public DateTime date { get; set; }

        }

    When I run this code I hoped I would see the Date formated to match my regional settings, but it is not. Why? If I look at Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern it shows "dd.MM.yyyy", but the text in the TextBlock is showing a date in the following format "d/MM/YYYY". If I run DateTime.Now.ToString() it shows in the correct format. Do anyone have an explanation? Can this be fixed without using a Converter?

    Wednesday, April 1, 2009 8:37 AM

Answers

  • If you're using ASP.NET and want the browser settings to be used for the Culture settings, you should do the following:
    1. set the following tag in the system.web section of the web.config:

    <GLOBALIZATION culture="auto" uiculture="auto" />
    2. Set the following parameters in the object tag of the silverlight control in the ASPX page:
    <param value="<%=System.Threading.Thread.CurrentThread.CurrentCulture.Name %>" name="Culture" />  
    <param value="<%=System.Threading.Thread.CurrentThread.CurrentUICulture.Name %>" name="UICulture" />  

     I used the same construction for my "auto translate" article on my blog and it works fine:
    http://web-snippets.blogspot.com/2009/03/automatically-translate-your.html

     

    Saturday, April 4, 2009 3:23 AM

All replies

  • The CurrentCulture property uses the regional setting of your system and not the language specified in the browser. Maybe that is causing the difference between the 2 methods in your case?

    Wednesday, April 1, 2009 12:32 PM
  • The language in the browser it set to norwegian. Regional settings is set to norway to, but CurrentUICulture is set to en-US. It seems like en-US is what is being used, but it doesn't help to change it. I have tried to set CurrentUICulture = CurrentCulture (In app.aspx.cs before loading the control), but it doesn't help. I have fixed this using a Converter, but what is strange is both the Convert and ConvertBack method has a CultureInfo argument which is set to "en-US" even if my CurrentCulture and CurrentUiCulture is set to "no-nb" (norwegian). Can someone explain this?

    Wednesday, April 1, 2009 2:12 PM
  • If you're using ASP.NET and want the browser settings to be used for the Culture settings, you should do the following:
    1. set the following tag in the system.web section of the web.config:

    <GLOBALIZATION culture="auto" uiculture="auto" />
    2. Set the following parameters in the object tag of the silverlight control in the ASPX page:
    <param value="<%=System.Threading.Thread.CurrentThread.CurrentCulture.Name %>" name="Culture" />  
    <param value="<%=System.Threading.Thread.CurrentThread.CurrentUICulture.Name %>" name="UICulture" />  

     I used the same construction for my "auto translate" article on my blog and it works fine:
    http://web-snippets.blogspot.com/2009/03/automatically-translate-your.html

     

    Saturday, April 4, 2009 3:23 AM