1

I have the following XAML:

<Window x:Class="String_Format.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="250">
    <StackPanel Margin="10">
        <TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, StringFormat=Date: {0:dddd, MMMM dd}}"/>
    </StackPanel>
</Window>

In the XAML designer in Visual Studio 2015 as well as in the running application, this displays as: "Date: Thursday, January 28". As I am on a German Windows 7 with regional settings set to "German (Germany)", I had actually expected: "Date: Donnerstag, Januar 28".

So I went and added the namespace

xmlns:c="clr-namespace:System.Globalization;assembly=mscorlib"

and altered my TextBlock to:

<TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, ConverterCulture={x:Static c:CultureInfo.CurrentCulture}, StringFormat=Date: {0:dddd, MMMM dd}}"/>

This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.

This has been asked before, but I wanted to do it inside XAML, without using code behind, if possible.

So what's wrong with my XAML? I'd like to understand.

Community
  • 1
  • 1
Kim Homann
  • 3,042
  • 1
  • 17
  • 20
  • 1
    Possible duplicate of [WPF XAML Bindings and CurrentCulture Display](http://stackoverflow.com/questions/991526/wpf-xaml-bindings-and-currentculture-display) – Cameron MacFarland Jan 28 '16 at 10:59
  • Not quite! Yes, this does answer then question for the easier solution. But I would still like to understand why I can't do it like this within XAML. I'm rather new to XAML and I think I'm missing out on some basic concept herre!? – Kim Homann Jan 28 '16 at 11:41
  • You can do it with a custom binding, which can be found here: http://stackoverflow.com/questions/5831455/use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl – SnowballTwo Jan 28 '16 at 11:46

2 Answers2

1

This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.

This is because the added code: ConverterCulture={x:Static c:CultureInfo.CurrentCulture} cannot be resolved at design time. This results in the invalid markup and lack of display. It is resolved at runtime, which is why your date displays in the correct language/culture.

You can only solve this by moving this into code where you can check whether it's runtime or design time.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Why can't it be resolved at design time? I mean, `s:DateTime.Now` can be resolved, why can't `c:CultureInfo.CurrentCulture` be? What's the difference? – Kim Homann Jan 28 '16 at 14:52
0

Checkout if setting the Language on the Window helps, I do this in the code behind of a window / usercontrol etc:

this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

unkreativ
  • 482
  • 2
  • 8