Wpf ContextMenu项目标题动态绑定未更新

时间:2017-10-25 08:32:53

标签: wpf binding contextmenu resourcedictionary

开发多语言应用。除了ContextMenu中的Headers之外,一切正常。

我的资源文件如下:

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

    <system:String x:Key="TbIconConnect">Connect</system:String>
    <system:String x:Key="TbIconConnecting">Connecting</system:String>
...

在App.xaml ResourceDictionary中声明:

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="Langs/English.xaml"/>
        <ResourceDictionary Source="Langs/Spanish.xaml"/>

    </ResourceDictionary.MergedDictionaries>

ContextMenu在MainWindow.xaml中声明:

<ContextMenu x:Shared="false" x:Key="SysTrayMenu">
    <MenuItem Foreground="Aqua" Header="{DynamicResource TbIconConnect}" Click="ContextMenuConnectClick" FontSize="14">
        <MenuItem.Icon>
            <Image Source="/Img/App/Connect.png" Width="15" Height="15"/>
        </MenuItem.Icon>
    </MenuItem>
...

以这种方式与TaskBarIcon联系:

<tb:TaskbarIcon x:Name="TBIcon"
    IconSource="{Binding ViewModel.Status, Converter={StaticResource TaskBarIconConverter}}"
    ContextMenu="{DynamicResource SysTrayMenu}"
    ToolTipText="{Binding ViewModel.TbIconToolTip, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</tb:TaskbarIcon>

Switch语言代码是:

public void SwitchLanguage(Language lang)
{
    ResourceDictionary dict = new ResourceDictionary();

    switch (lang)
    {
        case Language.English:
            dict.Source = new Uri(@"..\Langs\English.xaml", UriKind.Relative);
        break;
        case Language.Spanish:
            dict.Source = new Uri(@"..\Langs\Spanish.xaml", UriKind.Relative);
        break;
    }

    Application.Current.Resources.MergedDictionaries.Add(dict);
}

应用内的所有内容都会随时更改,包括子表单。但ContextMenu项目标题仍保留原始语言。

有什么想法吗?

0 个答案:

没有答案