1

Im am using a dynamic resource dictionaries to translate my GUI-elements. The Dictionaries are loaded on startup or could be changed during runtime. Works very well!

Now i have to add some GUI-elements programmatically .. But they will not update after changing the translation..

Here is how i "translate" the GUI-elements in XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:tests.Dialogs" x:Class="tests.Dialogs.Dlg_Main"
    xmlns:Translator="clr-namespace:tests.Translation"
    Title="{DynamicResource DLG_MAIN_TITLE}"
    Height="356" Width="711"
/>

This is my MenuItem Class which is added to a ListBox:

public class MenuItem : FrameworkElement
{
    public MenuItem (string resourceKey, ClickAction action)
    {
        Action=action;

        this.SetResourceReference(ContentProperty, resourceKey);   
    }

    public void DoAction ()
    {
        if (Action!=null)
        {
            Action();
        }
    }

    public delegate void ClickAction ();
    private ClickAction Action;

    public Object Content
    {
        get { return (Object)GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ContentProperty=DependencyProperty.Register("Content", typeof(Object), typeof(MenuItem), new PropertyMetadata("leer?"));
}

And this is the XAML Style for the ListBox:

    <SolidColorBrush x:Key="ItemBrush" Color="Transparent" />
    <SolidColorBrush x:Key="SelectedItemBrush" Color="Orange" />
    <Style x:Key="RoundedItem" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="ItemBorder" BorderBrush="Transparent" BorderThickness="8,0,0,0" Margin="0" Padding="5" Background="{StaticResource ItemBrush}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="10,0,0,0" Text="{Binding Content.Content, RelativeSource={RelativeSource TemplatedParent}}" FontSize="18" FontWeight="Normal" VerticalAlignment="Center" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ListBox used like this in XAML:

<ListBox
    x:Name="gui_listbox_menu"
    ItemContainerStyle="{StaticResource RoundedItem}"
    Background="Transparent"
    SelectionChanged="gui_listbox_menu_SelectionChanged"
    Margin="0,20,0,0"
    ItemsSource="{Binding MenuItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Dlg_Main}}}"
/>

After startup the MenuItems have the correct translation, so the Reference works?! but if i change the language during runtime only the MenuItems still showing the old language!

If i add an Button programmatically and set its ReferenceSource to the DynamicResource its content will change during runtime, too!

        Button btn_test=new Button();
        btn_test.Content="test";
        btn_test.Width=100;
        btn_test.Height=100;
        gui_tab_settings_grid.Children.Add(btn_test);
        btn_test.SetResourceReference(Button.ContentProperty, "DLG_MAIN_TITLE");

But my MenuItems do not change during runtime??!

Any help would be welcome!

slaesh
  • 16,659
  • 6
  • 50
  • 52
  • Perhaps your `MenuItem`s *don't work* as you call it, because you are trying to call the `SetResourceReference` method from the constructor before your `Resource`s are loaded. Try calling that later and seeing what happens. – Sheridan Aug 21 '14 at 08:32
  • Resources are loaded before! The correct loaded translation will be displayed after start up !! but if i change the translation during runtime only the MenuItem's will not change and stay in "last language" .. – slaesh Aug 21 '14 at 08:41
  • see [set-custom-markupextension-from-code](http://stackoverflow.com/questions/7489789/set-custom-markupextension-from-code) – pushpraj Aug 21 '14 at 08:49
  • 1
    @pushpraj, the question author has already stated *I have read that MarkUpExtensions cannot be added programmatically*. – Sheridan Aug 21 '14 at 08:53

1 Answers1

0

I don't know whats the difference, but here is the "new" MenuItem class..

It derives now ListBoxItem ..

public class MenuItem : ListBoxItem
{
    public MenuItem (string resourceKey, ClickAction action)
    {
        Action=action;

        this.SetResourceReference(ContentProperty, resourceKey);   
    }

    public void DoAction ()
    {
        if (Action!=null)
        {
            Action();
        }
    }

    public delegate void ClickAction ();
    private ClickAction Action;
}
slaesh
  • 16,659
  • 6
  • 50
  • 52