How to create grid in Win Store App using C#?

我是研究僧i 提交于 2019-12-08 06:40:46

问题


I have created this grid :

Grid grid = new Grid // new Grid
{
    Width = 1500,
    Height = 1500,
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Center,
    Background = new SolidColorBrush(Colors.Coral)
};

var columnDefinition = new ColumnDefinition { Width = new GridLength(200) }; // column
var columnDefinition1 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition2 = new ColumnDefinition { Width = new GridLength(200) };
var columnDefinition3 = new ColumnDefinition { Width = new GridLength(200) };

grid.ColumnDefinitions.Add(columnDefinition); // add column to grid
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
grid.ColumnDefinitions.Add(columnDefinition3);

var rowDefinition = new RowDefinition { Height = new GridLength(300) }; // row
var rowDefinition1 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition2 = new RowDefinition { Height = new GridLength(300) };
var rowDefinition3 = new RowDefinition { Height = new GridLength(300) };

grid.RowDefinitions.Add(rowDefinition); // add row to grid
grid.RowDefinitions.Add(rowDefinition1);
grid.RowDefinitions.Add(rowDefinition2);
grid.RowDefinitions.Add(rowDefinition3);

var textBlock1 = new TextBlock // new textBlock
{
    Text = "TextBox 1 ",
    FontSize = 20,
    Foreground = new SolidColorBrush(Colors.White),
    Width = 100
};

Grid.SetColumn(textBlock1, 0); 
Grid.SetRow(textBlock1, 0); 

var rec = new Rectangle(); // new Rectangle
{
    Width = 70;
    Height = 70;

    rec.Fill = linearGradientBrush;
}

Grid.SetColumn(rec, 1);
Grid.SetRow(rec, 0);
grid.Children.Add(textBlock1); 
grid.Children.Add(rec); 
MainGrid.Children.Add(grid); // Add grid to main grid

But all I can see is small rectangle of coral color (which is color of my grid) in the middle of the screen. I know it's easier to create grid using XAML, I just want to know how to do it in C#. Thanks for help


回答1:


Try using

rec.SetValue(Grid.RowProperty, 0);
rec.SetValue(Grid.ColumnProperty, 1);

Similarly for your textblock..




回答2:


The most obvious problem I see in your code is that you add the Grid children after you try to set the attached property. I believe the element needs to already be a child of the Grid object before you set the attached property. Otherwise, how would the static method know which Grid object's attached property should be set?

This code, an event handler subscribed to the Grid's Loaded event, works fine for me:

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    int rows = 2, columns = 3;

    for (int i = 0; i < rows; i++)
    {
        RowDefinition definition = new RowDefinition();

        definition.Height = new GridLength(1, GridUnitType.Star);
        grid.RowDefinitions.Add(definition);
    }

    for (int i = 0; i < columns; i++)
    {
        ColumnDefinition definition = new ColumnDefinition();

        definition.Width = new GridLength(1, GridUnitType.Star);
        grid.ColumnDefinitions.Add(definition);
    }

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
        {
            TextBlock text = new TextBlock();

            text.Text = string.Format("column: {0}, row: {1}", j, i);
            text.FontSize = 36;
            text.HorizontalAlignment = HorizontalAlignment.Center;
            text.VerticalAlignment = VerticalAlignment.Center;
            text.Margin = new Thickness(10, 10, 10, 10);
            grid.Children.Add(text);

            Grid.SetRow(text, i);
            Grid.SetColumn(text, j);
        }
}


来源:https://stackoverflow.com/questions/22474827/how-to-create-grid-in-win-store-app-using-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!