Scott Marlowe
Scott Marlowe

Reputation: 8025

How to dynamically add controls to a Silverlight Button?

I'm working on a Silverlight Polling control. My goal is to have a Button that contains other controls, so that when a user clicks on this combination, stuff happens.

Here's the basic XAML:

<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
   <StackPanel Grid.Row="1" Grid.Column="0" Height="Auto" Width="Auto" Style="{StaticResource StackPnl}">
      <TextBox Text="this is a text box" Style="{StaticResource Answer}" />
      <ProgressBar Style="{StaticResource PollProgress}" />
   </StackPanel>
</Button>

That works fine, but assume I don't have the StackPanel, etc, defined:

<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">

</Button>

But that I want to add the StackPanel, TextBox, and ProgressBar at runtime.

Looking at just the first hurdle that I've run into, how do I dynamically add the StackPanel to the Button control?

Alternatively, if someone knows of a better way to do this...

Upvotes: 1

Views: 4995

Answers (1)

Michael S. Scherotter
Michael S. Scherotter

Reputation: 10785

var panel = new StackPanel();

Button.Content = panel;

Upvotes: 3

Related Questions