在后面的代码中更改标签的 ContentStringFormat 属性

Changing the ContentStringFormat property of a Label in code behind

是否可以在代码中更改 Label 的 ContentStringFormat 属性?

拿这个XAML:

<Label ContentFormatString="Hello {0}" Content="John" x:Name="MyLabel" />

而这个 C#:

MyLabel.ContentFormatString = "Bye {0}";

调试时,您会看到 属性 的值实际上发生了变化,但这在 UI 中没有显示出来。

这可能吗?

可能不是很优雅,但是这个很管用:

var content = MyLabel.Content;
MyLabel.Content = null;
MyLabel.ContentStringFormat = "Bye {0}";
MyLabel.Content = content;