替换 vb.net 中的函数

Replace function in vb.net

我有两个变量 label 及其值“LABEL_NAME=”和 value 及其值“A_B” 我使用替换函数从 label 中删除“_”,并将其替换为 space,如

Dim label as String = "LABEL_NAME="
Dim value as String = "A_B"
label=label.Replace("_"," ")

并将其值赋给LABEL控件

dynamillabel.content=label & value

我希望结果为 "LABEL NAME=A_B" 但我得到

"LABEL NAME=AB"

我正在替换标签变量的_,它也删除了值变量的“_”。

编辑: 我想连接两个变量并将其分配给 Label.content 。但在连接之前,我想用 space 替换第一个变量 "_"。当我这样做时,它会删除第二个变量“_”,并在其上方删除我使用过的代码段。

WPF 中的下划线用作键盘快捷键的指示符。它取代了 winforms 中的 & 符号,如 XAML & 无效,并且为键盘快捷键

编写 & 会有点烦人

如果您希望在您的内容中使用下划线,您应该使用另一个下划线对下划线进行转义。

将您的代码更改为此,应该可以显示下划线:

Dim label as String = "LABEL_NAME="
Dim value as String = "A__B"
label=label.Replace("_"," ")

dynamillabel.content=label & value

您现在将看到下划线

这里也提出了类似的问题,您可以在其中使用自定义模板来禁用此功能

Disable WPF label accelerator key (text underscore is missing)

下划线在标签中是特殊的。你需要一个额外的 Replace():

Dim text = label & value
text = text.Replace("_", "__")    
dynamillabel.content=text

为什么它很特别在 MSDN 的 Label 文章中解释得很好,我只是引用它:

This class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls such as a TextBox. To assign a Label to a Control, set the Target property to the control that should get focus when the user presses the access key. Setting the target also causes Microsoft UI Automation to use the text of the label as the name of the targeted control. For more information, see Accessibility.

To set the access key, add an underscore before the character that should be the access key. If your content has multiple underscore characters, only the first one is converted into an access key; the other underscores appear as normal text.

考虑将 TextBlock 作为替代方案。