none
FrameworkElement.LanguageProperty.OverrideMetadata

    Question

  • Hi,

    as suggested in many articles I use

    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
        new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
    

    in the startup event to set the culture of my WPF application.
    However, this only works for the UI in my EXE project. It does not work for UserControls that originate from DLL projects. I don't know if it makes a difference but I'm using PRISM and I'm loading my UserControls by the help of MEF (Managed Extensibility Framework). The WPF UserControls from my DLL projects seem to ignore the culture.

    Thanks for any ideas,
    Guido

     

    Friday, December 23, 2011 8:33 AM

Answers

  • Ugh, I see. The problem is the Run element, this is not a FrameworkElement, it's a FrameworkContentElement. Its language property was registered with a default value of en-US and it cannot be overriden.

    I'm not sure how it works for you when setting the Language directly, that doesn't work for me. The only thing that works is setting the language right on the Run or removing the Run completely.

    I'd say this is a bug.

     

    Friday, December 23, 2011 12:27 PM
    Moderator
  • Hi!

    I was able to solve the Run issue with the following code at application startup. Doing this makes Run elements use the current culture while formatting binding values.

    Saludos amigos!

    Run.LanguageProperty.OverrideMetadata(typeof(Run), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

     
    Monday, May 13, 2013 10:04 AM
  • Hi Guido Kraus,

    In addition to Mike's comments, I think setting the language property of the root visual can effectively affects all elements in the tree. The LanguageProperty is an attached property which will inherit the value from the parent element.

    So setting the Language property of every Window/Page would be a good idea.

    @Mike, not sure why it doesn't work in your scenario, could you share more details about that? Thank you.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Guido Kraus Wednesday, December 28, 2011 7:15 AM
    Monday, December 26, 2011 2:14 AM
    Moderator

All replies

  • I have to correct myself: FrameworkElement.LanguageProperty.OverrideMetadata does work for DLL projects. However, I have a StatusBarItem and inside the StatusBarItem the current culture seems to be ignored. Here is my StatusBar:

            <StatusBar DockPanel.Dock="Bottom">
                <StatusBarItem x:Name="InfoStatusBarItem">
                    <TextBlock>
                        <Run Text="{Binding Path=Partners.Count, Mode=OneWay, StringFormat=\{0:N0\}}" />
                    </TextBlock>
                </StatusBarItem>
                <StatusBarItem>
                    <Label Content="{Binding Path=Partners.Count, StringFormat=\{0:N\}}" />
                </StatusBarItem>
            </StatusBar>
    

    Neither my TextBlock nor my Label respect the current culture. Interestingly the TextBlock shows en-US format (e.g. 1,000) and the Label shows an unformatted number (1000). What's wrong with my StatusBar?

    Guido

    Friday, December 23, 2011 9:15 AM
  • Friday, December 23, 2011 9:21 AM
  • Sorry, but this does not relate to my StatusBar problem.
    Friday, December 23, 2011 9:52 AM
  • "Neither my TextBlock nor my Label respect the current culture"

    What's your current culture?

    "Interestingly the TextBlock shows en-US format (e.g. 1,000) and the Label shows an unformatted number (1000)."

    This is a different problem. The StringFormat is ignore in the case of the label. StringFormat is only used if a conversion to string is required like in the case of the Text property of the Run. But the Content property of Label is object and so the binding won't do any conversion, it will simply transfer an integer value to the Content property.

    Friday, December 23, 2011 10:55 AM
    Moderator
  • Thanks for the explanation of the Label behavior!

    I'm testing different cultures: de-DE and de-CH
    My StatusBar lives inside a UserControl and other Labels / Textblocks outside the StatusBar (but inside the UserControl) work as expected. Even more confusing: When I explicitly set the language

    this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(System.Threading.Thread.CurrentThread.CurrentCulture.ToString())
    

    in the constructor of the UserControl then even the StatusBar works as expected.

    Guido

     

    Friday, December 23, 2011 11:54 AM
  • Ugh, I see. The problem is the Run element, this is not a FrameworkElement, it's a FrameworkContentElement. Its language property was registered with a default value of en-US and it cannot be overriden.

    I'm not sure how it works for you when setting the Language directly, that doesn't work for me. The only thing that works is setting the language right on the Run or removing the Run completely.

    I'd say this is a bug.

     

    Friday, December 23, 2011 12:27 PM
    Moderator
  • Thanks for the insight. Maybe I'll file a bug at connect.microsoft.com

     

    Friday, December 23, 2011 3:18 PM
  • Hi Guido Kraus,

    In addition to Mike's comments, I think setting the language property of the root visual can effectively affects all elements in the tree. The LanguageProperty is an attached property which will inherit the value from the parent element.

    So setting the Language property of every Window/Page would be a good idea.

    @Mike, not sure why it doesn't work in your scenario, could you share more details about that? Thank you.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Guido Kraus Wednesday, December 28, 2011 7:15 AM
    Monday, December 26, 2011 2:14 AM
    Moderator
  • "not sure why it doesn't work in your scenario, could you share more details about that? "

    Unfortunately I cannot reproduce it now, it's possible that I did something wrong like copy pasting OP's example without replacing CurrentCulture.IetfLanguageTag with something like "de-DE".

    Monday, December 26, 2011 7:52 AM
    Moderator
  • Hi Guido Kraus,

    Just checking in the see if you need any further assistance on this question.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 28, 2011 3:16 AM
    Moderator
  • Hi!

    I was able to solve the Run issue with the following code at application startup. Doing this makes Run elements use the current culture while formatting binding values.

    Saludos amigos!

    Run.LanguageProperty.OverrideMetadata(typeof(Run), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

     
    Monday, May 13, 2013 10:04 AM
  • Yes, this seems to work! Thanks, Guido
    Monday, May 13, 2013 11:09 AM