SilverLight中的网格布局

时间:2013-02-28 23:06:52

标签: c# silverlight

我想要一个用于显示搜索结果的网格布局。网格应该有头条好处所有者,商业运营商和注册所有者。然后应在右侧标题下显示结果。

我正在努力实现这种布局:

enter image description here

但这就是我得到的:

enter image description here

我的C#/ SilverLight代码:

// Create a 3 column grid
StackPanel deptStackPanel = new StackPanel();
deptStackPanel.Margin = new Thickness(10);
stackPanelSearchResults.Children.Add(deptStackPanel);
Grid.SetColumn(deptStackPanel, 3);
Grid.SetRow(deptStackPanel, 3);

// Add headlines for theese columns
TextBlock deptListHeadingBeneficialOwner = new TextBlock();
deptListHeadingBeneficialOwner.Text = "Beneficial Owner";

TextBlock deptListHeadingCommercialOperator = new TextBlock();
deptListHeadingCommercialOperator.Text = "Commercial Operator";

TextBlock deptListHeadingRegisteredOwnerName = new TextBlock();
deptListHeadingRegisteredOwnerName.Text = "Registered Owner";

deptStackPanel.Children.Add(deptListHeadingBeneficialOwner);
deptStackPanel.Children.Add(deptListHeadingCommercialOperator);
deptStackPanel.Children.Add(deptListHeadingRegisteredOwnerName);
Grid.SetColumn(deptListHeadingBeneficialOwner, 0);
Grid.SetColumn(deptListHeadingCommercialOperator, 1);
Grid.SetColumn(deptListHeadingRegisteredOwnerName, 2);

2 个答案:

答案 0 :(得分:0)

从xaml文件或后端在布局网格中添加三个网格。将堆栈面板放在其中,然后为数据添加文本块。您只是将文本块添加到另一个下面。

答案 1 :(得分:0)

我找到了自己的解决方案:

// Create the Grid
            Grid myGrid = new Grid();
            myGrid.Width = 400;
            myGrid.Margin = new Thickness(9, 0, 0, 0);
            myGrid.HorizontalAlignment = HorizontalAlignment.Left;
            myGrid.VerticalAlignment = VerticalAlignment.Top;
            myGrid.ShowGridLines = true;

            // Define the Columns
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();
            myGrid.ColumnDefinitions.Add(colDef1);
            myGrid.ColumnDefinitions.Add(colDef2);
            myGrid.ColumnDefinitions.Add(colDef3);

            // Define the Rows
            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();
            RowDefinition rowDef4 = new RowDefinition();
            myGrid.RowDefinitions.Add(rowDef1);
            myGrid.RowDefinitions.Add(rowDef2);
            myGrid.RowDefinitions.Add(rowDef3);
            myGrid.RowDefinitions.Add(rowDef4);

            // Add the second text cell to the Grid
            TextBlock txtBeneficialOwner = new TextBlock();
            txtBeneficialOwner.Text = "Beneficial Owner";
            txtBeneficialOwner.FontWeight = FontWeights.Bold;
            Grid.SetRow(txtBeneficialOwner, 0);
            Grid.SetColumn(txtBeneficialOwner, 0);

            // Add the third text cell to the Grid
            TextBlock txtCommercialOperator = new TextBlock();
            txtCommercialOperator.Text = "Commercial Operator";
            txtCommercialOperator.FontWeight = FontWeights.Bold;
            txtCommercialOperator.Margin = new Thickness(9, 0, 0, 4);
            Grid.SetRow(txtCommercialOperator, 0);
            Grid.SetColumn(txtCommercialOperator, 1);

            // Add the fourth text cell to the Grid
            TextBlock txtRegisteredOwnerName = new TextBlock();
            txtRegisteredOwnerName.Text = "Registered Owner";
            txtRegisteredOwnerName.FontWeight = FontWeights.Bold;
            txtRegisteredOwnerName.Margin = new Thickness(9, 0, 0, 4);
            Grid.SetRow(txtRegisteredOwnerName, 0);
            Grid.SetColumn(txtRegisteredOwnerName, 2);

            // Add the TextBlock elements to the Grid Children collection
            myGrid.Children.Add(txtBeneficialOwner);
            myGrid.Children.Add(txtCommercialOperator);
            myGrid.Children.Add(txtRegisteredOwnerName);
            stackPanelSearchResults.Children.Add(myGrid);