【发布时间】:2015-02-28 00:47:22
【问题描述】:
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# 中显示它?
【问题讨论】:
-
删除所有内容并使用正确的 XAML。
-
我已经知道有时 XAML 是最好的解决方案,但我想知道这一点,因为我很好奇。
标签: wpf xaml window parent-child mainwindow