Tuesday, February 26, 2008

Set Label Text in XAML and C#

I made a UI in XAML using the designer... one of the first things I wanted to do was change the text of a Label from C# code. I was surprised to see that in WPF, a Label has no Text property:This is in contrast to a Windows Form, where Label does have a Text property:


The answer, of course, is that WPF has a class TextBlock that seems to be a more dynamic version of Label. It has a Text property:

Just as reference, when changing properties of UI components in WPF,

protected void ChangeText(string s)
new TextSetterDelegate(TextSetter), textBlock, s);
delegate void TextSetterDelegate(TextBlock tb, string text);
protected void TextSetter(TextBlock tb, string text)
tb.Text = text;

1 comment:

Neil said...

Label.Content is what you're looking for.