如何使用 C# 在 Win Store App 中创建网格? [英] How to create grid in Win Store App using C#?
问题描述
我已经创建了这个网格:
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
但我只能看到屏幕中间的珊瑚色小矩形(这是我网格的颜色).我知道使用 XAML 创建网格更容易,我只想知道如何在 C# 中做到这一点.感谢帮助
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
推荐答案
我在你的代码中看到的最明显的问题是你添加了 Grid
子项 after设置附加属性.我相信在设置附加属性之前,该元素需要已经是 Grid
对象的子元素.否则,静态方法如何知道应该设置哪个 Grid
对象的附加属性?
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?
此代码是订阅 Grid
的 Loaded
事件的事件处理程序,对我来说效果很好:
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);
}
}
这篇关于如何使用 C# 在 Win Store App 中创建网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!