在后面的代码中创建Resources.resx中的字符串绑定 [英] Create binding to string in Resources.resx from code behind

查看:154
本文介绍了在后面的代码中创建Resources.resx中的字符串绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在XAML中制作一个标签,其内容绑定到资源文件中的一个字符串。我已经实施本地化,并确认当我更改语言时,标签的内容将相应更新。



现在我需要从代码中执行相同操作。



以下是XAML的味道:

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

< WrapPanel VerticalAlignment =TopBackground ={Binding AxisDataColorCode}Margin =2,2,2,0>
< Label x:Name =lbl_General_MachineTypeContent ={Binding GUI_MachineType,Source = {StaticResource Resources}}FontSize =20/>
< Label x:Name =lbl_General_MachineTypeResultContent ={Binding MachineBaseType}FontSize =20/>
< / WrapPanel>

< WrapPanel ....

尝试在代码中重新创建-behind我有以下:

  Binding BgColorBinding = new Binding(AxisDataColorCode); 

//这里需要改变。我已经尝试过一堆事情,没有运气。
绑定GUI_MachineTypeBinding = new Binding(GUI_MachineType);
GUI_MachineTypeBinding.Source = Properties.Resources.GUI_MachineType;

Binding MachineBaseTypeBinding = new Binding(MachineBaseType);


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

//这根本不行。帮帮我!
Label_MachineType.SetBinding(Label.ContentProperty,GUI_MachineTypeBinding);

//这个工程!但它不是一个绑定,不会更新...
// Label_MachineType.Content = Properties.Resources.GUI_MachineType;

标签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);

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



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



我找不到任何绑定到他们的属性的例子。资源。无论从任何地方的代码。






解决方案: p>

感谢Henka!

 绑定GUI_MachineTypeBinding = new Binding(GUI_MachineType); 
GUI_MachineTypeBinding.Source = Application.Current.FindResource(资源);
....
Label_MachineType.SetBinding(Label.ContentProperty,GUI_MachineTypeBinding);


解决方案

不要通知您应该创建自定义扩展并将 WeakReference 保存到 DependencyProperty 中,并在文化更改时更新值,我提出使用其他解决方案,看看这篇文章。高级WPF本地化


I started by making a label in XAML with its content bound to a string in my resources file. I've implemented localization and confirm that when I change languages, the label's content updates accordingly.

Now I need to do the same from code behind.

Here is a taste of the 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....

Attempting to recreate this in code-behind I have the following:

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

My other bindings work fine, because I've just tied them to properties in code behind that implement property changed notification.

Trying to bind to any of the keys in my resources however, gives me nothing. The label's content is simply blank, and there are no errors in my debug output window.

I can't find any examples of anyone binding to their Properties.Resources.Whatever from code behind anywhere.


The solution:

Thanks Henka!

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

解决方案

If you set the Binding from code behind the UI will not be notified you should create your custom extension and save a WeakReference to the DependencyProperty and update the value when the culture changed, i propose an other solution to use, have a look at this article.Advanced WPF Localization

这篇关于在后面的代码中创建Resources.resx中的字符串绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆