以编程方式设置动态资源但是当ResourceDictionary更改时FrameWorkElement不会更新?

时间:2014-08-21 06:22:27

标签: c# wpf xaml translation markup-extensions

我正在使用动态资源字典来翻译我的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"
/>

这是我的MenuItem类,它被添加到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?"));
}

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

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

启动后,MenuItems有正确的翻译,所以参考有效吗?! 但如果我在运行时更改语言,只有MenuItems仍显示旧语言!

如果我以编程方式添加Button并将其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在运行时没有改变??!

欢迎任何帮助!

1 个答案:

答案 0 :(得分:0)

我不知道其中的差异,但这里是&#34; new&#34; 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;
}