如何手动在资源文件中引用DataGridViewColumn.HeaderText?

3

为了进行交易,我创建了资源文件来替换winforms组件的文本属性。

然而,似乎我无法在资源文件中正确引用DataGridViewColumn.HeaderText属性;但是我可以在代码中更改其HeaderText属性,但不能在资源文件中更改(它适用于其他组件...)

我也尝试过:

DataGridViewColumn.HeaderText = "test1";
DataGridViewColumn.HeaderCell.Value = "test2";
DataGridView.Columns[1].HeaderText = "test3";

代码在调用时可以正常工作,但将其放入资源文件中后无法正常工作。

你是说你在*.resx文件中写了一些C#代码?我知道我们只是将字符串、图像、图标和其他类似的资源放在resx文件中。 - RBT
2
您可以使用代码从资源文件中设置 HeaderTextDataGridViewColumn.HeaderText = YourApplicationNamespace.Properties.Resources.DataGridViewHeaders.Column1 - 其中 DataGridViewHeaders 是资源文件的名称,Column1 是文本的键。 - Fabio
我在我的表单上设置了localize = true,以便能够将我的标签翻译成其他语言,并且我手动更新每种语言的每个文本标签(字符串类型,在左列中放置button1.text,右列中放置翻译后的值)。 - Théo
1个回答

2
如果您使用卫星程序集来保存本地化文本,则可以像这样操作:
//namespacaes to be imported at the top of your code file
using System.Resources;
using System.Reflection; 

//source code for your method
ResourceManager resourceManager = new ResourceManager("TestSatelliteAssembly.Resources.LocalizedResources",Assembly.GetExecutingAssembly());
DataGridViewColumn.HeaderText = resourceManager.GetString("lblUserNameText");

lblUserNameText是您要本地化的文本的键。 TestSatelliteAssembly是您卫星程序集的名称。

您可以在我的博客这里阅读有关卫星程序集的更多信息。


2
你可以摆脱硬编码的键 - Visual Studio将为它们生成属性 - 并为您提供良好可维护的智能感知帮助。 - Fabio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接