简体   繁体   English

在按钮内添加StackPanel和一个以上的TextBlock

[英]Add StackPanel and more than one TextBlock inside Button

I want to add a number of TextBlock s inside a Button . 我想在Button添加多个TextBlock How can I add a number of them, along with StackPanel s or Canvas es, in C#, as shown below in XAMAL 如何在C#中添加其中的一些以及StackPanelCanvas ,如XAMAL所示

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

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;
    }
}

Maybe you should think about an enclosing control of csharpfolk's answer. 也许您应该考虑封闭csharpfolk的答案。 This would help to get a reuseable control. 这将有助于获得可重用的控件。

The text strings are good to use as a dependency property. 文本字符串可以很好地用作依赖项属性。 :) :)

Regards, - Tobbo 问候-Tobbo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM