wpfconvertersuiculture

wpf converter culture != en-US


Hi I have a really strange effect here:

I have a main window, which content is filled with a complete visual tree containing the form presentation.

First form to show is the Parent form, which allows to call a child form.

When I load the child form, the Parent is basically backup up into a stack, then the new content is set to the window.

When I close the child form, the backuped up parent is set back as the window content and the child window is dropped.

Now for the problem: I load a collection of objects into a listbox. The listbox uses textblocks for displaying the objects' properties (FirstName, LastName, PhoneNumber,MobileNumber, Email), and after each textblock, a comma is contained in the Xaml to separate the entries.

BUT: if a textblock is empty, the corresponding comma must not be shown, like below:

enter image description here

I managed to do this using a converter that anaylses which commas to show.

This work perfectly, but when I show a child form and closes it, the display is corrupted regarding comma visibility:

enter image description here

I made two observations:

  1. The corrupted display only happens when I have a UI Culture different form en-US (de-DE in my case)
  2. I placed some traces in the converter code and I see that the converter is called after opening (and closing) the child form when I use de-DE, but not when I use en-US.

Is there any idea what can cause such effects? And how I can get around them?

Regards


Solution

  • Finally, I found that setting Window.Language was the problem.

    I do this in the mainwindows constructor:

    this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name);
    

    This is necessary to get correct display for numbers for example.

    But now I issue the following block, and all works in all cultures. [During startup, I check the default Ui culture, and if I set it to any other, I set App.CultureChanged accordingly]:

        if (!App.CultureChanged )
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
        this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name);