网格线不会显示

时间:2013-06-30 14:37:43

标签: c# wpf

网格线不会显示在runt-time上我似乎可以看到我出错的地方。这是一个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();
    }

}

}

1 个答案:

答案 0 :(得分:3)

问题可能还有“网格不会显示”。

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

private void CreateGrid()
{
    ...
    Content = myGrid;
}