0
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        System.Windows.Controls.Button b = new System.Windows.Controls.Button(); 
        System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle(); 
        r.Width = 40; 
        r.Height = 40; 
        r.Fill = System.Windows.Media.Brushes.Black; 
        b.Content = r; // Make the square the content of the Button
        this.AddChild(b);
    }
}

WPF 4 ブックのボタンのコードがあり、(XAML からではなく) ここから表示したいのですが、ボタン 'b' をメイン ウィンドウの子として追加すると、例外と情報が表示されます: ContentControl のコンテンツ単一の要素でなければなりません。

どうすればC#で表示できますか?

4

1 に答える 1