0

网格线不会在运行时显示我似乎可以看到我哪里出错了。这是一个 WPF 窗口应用程序。我只想使用 C# 代码生成列和行。任何想法为什么这不起作用?

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Navigation;
      using System.Windows.Shapes;

      namespace generator
       {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }

    private void CreateGrid()
    {

        ColumnDefinition myColum =new ColumnDefinition();
        RowDefinition myRow = new RowDefinition();
        ColumnDefinition myColum1 = new ColumnDefinition();
        RowDefinition myRow1 = new RowDefinition();

        Grid myGrid = new Grid();
        myRow.Height = new GridLength(1, GridUnitType.Star);
        myColum.Width = new GridLength(1, GridUnitType.Star);
        myRow1.Height = new GridLength(1, GridUnitType.Star);
        myColum1.Width = new GridLength(1, GridUnitType.Star);

        myGrid.ColumnDefinitions.Add(myColum);
        myGrid.RowDefinitions.Add(myRow);
        myGrid.ColumnDefinitions.Add(myColum1);
        myGrid.RowDefinitions.Add(myRow1);
        myGrid.ShowGridLines = true;

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        CreateGrid();
    }

}

}

4

1 回答 1

3

该问题也可以阅读“网格不会显示”。

您正在创建一个新Grid实例,但您从未将它添加到可视化树中。您可以将其分配给 Window 的Content属性:

private void CreateGrid()
{
    ...
    Content = myGrid;
}
于 2013-06-30T14:42:15.703 回答