我正在开发一个 WPF 应用程序,我想从 c# 代码而不是 XML 创建整个窗口。
我尝试了以下代码,但没有任何反应,网格不显示。我错过了什么?是否可以这样做或者有其他解决方案吗?
public MainWindow()
{
Grid grd = new Grid();
grd.Margin = new System.Windows.Thickness(10, 10, 10, 0);
grd.Background = new SolidColorBrush(Colors.White);
grd.Height = 104;
grd.VerticalAlignment = System.Windows.VerticalAlignment.Top;
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
RowDefinition row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
InitializeComponent();
}