【问题标题】:Create binding to string in Resources.resx from code behind从后面的代码创建绑定到 Resources.resx 中的字符串
【发布时间】:2016-06-15 19:45:57
【问题描述】:

我首先在 XAML 中创建一个标签,其内容绑定到我的资源文件中的字符串。我已经实现了本地化,并确认当我更改语言时,标签的内容也会相应更新。

现在我需要从后面的代码中做同样的事情。

这里是 XAML 的体验:

<Grid Background="#FF313131">
  <ScrollViewer>
    <StackPanel x:Name="GeneralTab_StackPanel">

      <WrapPanel VerticalAlignment="Top" Background="{Binding AxisDataColorCode}" Margin="2,2,2,0">
        <Label x:Name="lbl_General_MachineType" Content="{Binding GUI_MachineType, Source={StaticResource Resources}}" FontSize="20" />
        <Label x:Name="lbl_General_MachineTypeResult" Content="{Binding MachineBaseType}" FontSize="20" />
      </WrapPanel>

      <WrapPanel....

尝试在代码隐藏中重新创建它,我有以下内容:

Binding BgColorBinding = new Binding("AxisDataColorCode");

// Something needs to change here. I've tried a bunch of things already with no luck.
Binding GUI_MachineTypeBinding = new Binding("GUI_MachineType");
GUI_MachineTypeBinding.Source = Properties.Resources.GUI_MachineType;

Binding MachineBaseTypeBinding = new Binding("MachineBaseType");


Label Label_MachineType = new Label();
Label_MachineType.Name = "lbl_General_MachineType";
Label_MachineType.FontSize = 20;

// This does not work at all. Help!
Label_MachineType.SetBinding(Label.ContentProperty, GUI_MachineTypeBinding);

// this works! but it's not a binding and doesn't update...
// Label_MachineType.Content = Properties.Resources.GUI_MachineType;

Label Label_MachineTypeResult = new Label();
Label_MachineTypeResult.Name = "lbl_General_MachineTypeResult";
Label_MachineTypeResult.FontSize = 20;
Label_MachineTypeResult.SetBinding(Label.ContentProperty, MachineBaseTypeBinding);

WrapPanel MachineTypeWrapPanel = new WrapPanel();
MachineTypeWrapPanel.Name = "MachineTypeWrapPanel";
MachineTypeWrapPanel.VerticalAlignment = System.Windows.VerticalAlignment.Top;
MachineTypeWrapPanel.Margin = new Thickness(2, 2, 2, 0);
MachineTypeWrapPanel.SetBinding(WrapPanel.BackgroundProperty, BgColorBinding);
MachineTypeWrapPanel.Children.Add(Label_MachineType);
MachineTypeWrapPanel.Children.Add(Label_MachineTypeResult);

我的其他绑定工作正常,因为我刚刚将它们绑定到实现属性更改通知背后的代码中的属性。

但是,尝试绑定到我的资源中的任何键都没有给我任何东西。标签的内容只是空白,在我的调试输出窗口中没有错误。

我无法从任何地方的代码中找到任何人绑定到他们的 Properties.Resources.Whatever 的示例。


解决办法:

谢谢亨卡!

Binding GUI_MachineTypeBinding = new Binding("GUI_MachineType");
GUI_MachineTypeBinding.Source = Application.Current.FindResource("Resources");
....
Label_MachineType.SetBinding(Label.ContentProperty, GUI_MachineTypeBinding);

【问题讨论】:

    标签: c# wpf data-binding code-behind resx


    【解决方案1】:

    如果您从 UI 后面的代码设置绑定将不会收到通知,您应该创建自定义扩展并将 WeakReference 保存到 DependencyProperty 并在文化更改时更新值,我建议使用其他解决方案,看看这篇文章吧。Advanced WPF Localization

    【讨论】:

    • 我不确定我是否同意这一点。我的本地化实现与您链接到的非常相似。我正在通过ResourceProvider_DataChanged 事件在文化变化时更新属性和依赖属性。但是,似乎没有必要为资源字典中的键创建中间绑定。
    • 尝试将绑定源设置为 Properties.Resources 而不是属性本身
    • 不起作用。 “...resources 是一种类型,在给定的上下文中无效”。同样在查看了您链接的解决方案中的代码后,我确定它也会有同样的问题。演示中的任何内容都不是按程序创建的,它们都绑定到 XAML 中的静态资源。
    • 尝试在你的应用资源中定义StaticResourceResources,并将Binding源设置为Application.Current.FindResource()
    • 你是个天才!有效!我已经用解决方案更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2013-11-27
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多