Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't MenuItems work with DynamicResource?

The main menu of my program uses a ContextMenu composed of MenuItems. During the localization of my program (using Resource Dictionaries), I set a DynamicResource as the Header of each one of my MenuItems. Strangely DynamicResource compiles, but doesn't seem to affect any change during localization (the language on the Headers does not change).

Example of a MenuItem:

//I'm not sure if the x:Name or the PlacementRectangle is interfering with anything...
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="{DynamicResource open}" />
</ContextMenu>

What are the constraints of the MenuItem control? Is it supposed to work with DynamicResource? My overall goal is to localize these strings, how do I do that?

This program is in WPF. Thank you.

UPDATE: This is how my Resource Dictionaries are referenced in my App.xaml file:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="Lang.en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
    <ResourceDictionary>
<Application.Resources>

UPDATE 2: The example string in my English Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="open">Open</sys:String>
</ResourceDictionary>

Update 3: An example function for how I change the current Resource Dictionary to Spanish:

private void spanishChange_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Resources.MergedDictionaries.Clear();

    Application.Current.Resources.MergedDictionaries.Add(
            (ResourceDictionary)Application.LoadComponent(new Uri("LangspES.xaml", UriKind.Relative)));

    LanguageChange.FireLanguageChanged();
}
like image 827
Eric after dark Avatar asked Dec 11 '13 20:12

Eric after dark


1 Answers

Have you added LANGUAGE.xaml file to App.ResourceDictionary or control ResourceDictionary?

e.g.

<Application.Resources>
    <ResourceDictionary Source="LANGUAGE1.xaml" />
    <ResourceDictionary Source="LANGUAGE2.xaml" />
</Application.Resources>

If not how are you referencing your resource dictionaries?

Update:

If you change

<MenuItem Header="{DynamicResource open}" />

to

<MenuItem Header="{StaticResource open}" />

Does it then work ? Or even does

<TextBox DockPanel.Dock="Top" Text="{StaticResource open}" />

work ?

Seemingly your xaml should work, which makes me wonder have you setup localisation correctly in your app?

For how to setup localisation in .net 4.5 see this msdn link

like image 190
Paul Zahra Avatar answered Oct 11 '22 11:10

Paul Zahra