Skip Navigation LinksHome / Tips / View Tip

Tip: How to set the text of a Button?

+ Add to SilverlightShow Favorites
0 comments   /   posted by Denislav Savkov on Sep 04, 2008
(0 votes)
Categories: Controls and UI

In Silverlight the Button, as some other controls does not have the old well known Text property. It is replaced by a new property named Content of type object. The idea is to remove the limitation to show only strings as content of a control. Now with this new approach you can put whatever you wish inside of a Button. This makes the presentation layer extremely powerful. However if you provide a regular string for the Content then the result will be exactly as if we used the Text property.


<Button x:Name="simpleButton" Content="Click Me" />
<Button x:Name="advancedButton">
        <CheckBox Content="Check box on a button"></CheckBox>


Button simpleButton = new Button();
simpleButton.Content = "Click Me";
Button advancedButton = new Button();
CheckBox checkBox = new CheckBox();
checkBox.Content = "Check box on a button";
advancedButton.Content = checkBox;

As you can see the first button named simpleButton uses plain text 'Click Me' for its content, while the second button named advancedButton hosts check box control with the text 'Check box on a button'.

That's it!




Comments RSS RSS
No comments

Add Comment


Please add 5 and 1 and type the answer here:

Join the free SilverlightShow webinar 'Silverlight's Power Features: Data Binding in Action' on August 19th 2010, 10:00 am PDT.
In this session Gill Cleeren will make with a complete overview of the data binding features Silverlight 4 has to offer. He will discuss how data binding is the enabler for applying the MVVM pattern and commanding, and will end up reviewing Blend's support for data binding, including the use of design-time data. Read more | Register Now (hide this)