Puskarkc007
Puskarkc007

Reputation: 174

Add StackPanel and more than one TextBlock inside Button

I want to add a number of TextBlocks inside a Button. How can I add a number of them, along with StackPanels or Canvases, in C#, as shown below in XAMAL

<Button>
    <StackPanel>
         <TextBlock Text="ABC"/>
         <TextBlock Text="DEF"/>
    </StackPanel>
</Button>

Upvotes: 1

Views: 1444

Answers (2)

Tobonaut
Tobonaut

Reputation: 2483

Maybe you should think about an enclosing control of csharpfolk's answer. This would help to get a reuseable control.

The text strings are good to use as a dependency property. :)

Regards, - Tobbo

Upvotes: 0

csharpfolk
csharpfolk

Reputation: 4290

It's easy:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();

        var tb1 = new TextBlock() { Text = "TextBlock 1" };
        var tb2 = new TextBlock() { Text = "TextBlock 2" };

        var stackPanel = new StackPanel();
        stackPanel.Children.Add(tb1);
        stackPanel.Children.Add(tb2);

        var button = new Button() { Content = stackPanel };

        this.Content = button;
    }
}

Upvotes: 1

Related Questions