1

GUI要素を翻訳するために動的リソース辞書を使用しています。辞書は起動時に読み込まれるか、実行時に変更される可能性があります。非常にうまく機能します!

今、プログラムでいくつかの GUI 要素を追加する必要があります..しかし、翻訳を変更した後、それらは更新されません..

XAML で GUI 要素を「変換」する方法は次のとおりです。

<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"
/>

これは、ListBox に追加される MenuItem クラスです。

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?"));
}

これは、ListBox の XAML スタイルです。

    <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>

XAML で次のように使用される ListBox:

<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}}}"
/>

起動後、MenuItems は正しい翻訳を持っているので、参照は機能しますか?! しかし、実行時に言語を変更すると、MenuItems だけが古い言語を表示します!

ボタンをプログラムで追加し、その ReferenceSource を DynamicResource に設定すると、その内容も実行時に変更されます!

        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");

しかし、私のMenuItemsは実行時に変更されません??!

どんな助けでも大歓迎です!

4

1 に答える 1

0

違いはわかりませんが、ここに「新しい」MenuItemクラスがあります..

それは今 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;
}
于 2014-08-21T15:47:02.527 に答える