我必须为WP8.SDk创建一个表。
我正在为此目的使用网格。我创建了一个动态的行和列网格。
现在我想设置背景图片以使标题不同..
我的C#代码在这里。
private void table_click(object sender, RoutedEventArgs e)
{
//Creation of Grid
Grid tablegrid = new Grid();
tablegrid.Height = double.NaN;
tablegrid.Width = 454;
tablegrid.Margin = new Thickness(0, 66, 0, 0);
tablegrid.ShowGridLines = true;
tablegrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
tablegrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
//Table Rows and Columns Definition
string[] tablerow = new string[] { "Submit Report", "Something", "New" };
string[] tablecol = new string[] { "Mansab", "Arsal", "Ali" };
int i, j;
for (i = 0; i < tablerow.Length; i++)
{
RowDefinition gridrows = new RowDefinition();
gridrows.Height = new GridLength(66);
if (i == 0)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Resources/Images/top_bar_bg.png", UriKind.Relative));
//gridrows.Background = brush; //This not works
}
tablegrid.RowDefinitions.Insert(i, gridrows);
}
for (j = 0; j < tablecol.Length; j++)
{
ColumnDefinition gridCol = new ColumnDefinition();
tablegrid.ColumnDefinitions.Insert(j, gridCol);
}
tablegrid.ShowGridLines = true;
ContentPanel.Children.Add(tablegrid);
}
我的表格如下:
http://www.4shared.com/download/SlNX8mbpba/Table.PNG
但我必须做这样的事情:
http://www.4shared.com/download/j-qT-kGVce/table2.PNG
如何动态指定每行的背景?
答案 0 :(得分:1)
您可以使用ColumnHeaderStyle属性来设置列标题与内容行的不同。您是否可以指定每次都设置每行BG颜色的需要。如果不是特定需要,可以使用datagrid的AlternateRowStyle属性。
答案 1 :(得分:1)
在WPF中,Grid
没有“单元格”的概念,因此您无法为“单元格”的背景着色。而不是这个,你可以在你的单元格中放置一些东西并设置该控件的背景。例如,您可以为“标题”文本的TextBlock.Background
属性着色。
或者,您可以将StackPanel
或类似内容放入单元格中,并将TextBlock
放入其中,然后为StackPanel.Background
属性着色。
更新&gt;&gt;&gt;
就个人而言,我会说你选择了一种粗糙的方法来尝试实现你的要求。我认为您更容易使用Grid
仅用于标题行,而ListBox
用ItemsTemplate
包含另一个Grid
用于数据行。您可以使用Grid.IsSharedSizeScope
Attached Property来保持列彼此一致。
对于您当前的“图片”问题,您甚至可以在Image
上方显示ListBox
,然后在其顶部覆盖标题Grid
。
答案 2 :(得分:0)
我通过这样做解决了这个问题。
private void table_click(object sender, RoutedEventArgs e)
{
//Creation of Grid
Grid tablegrid = new Grid();
tablegrid.Height = double.NaN;
tablegrid.Width = double.NaN;
tablegrid.Margin = new Thickness(0, 66, 0, 0);
// tablegrid.ShowGridLines = true;
tablegrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
tablegrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
//Table Rows and Columns Definition
string[] tablerow = new string[] { "Submit Report","Arsal","AA","Mansab","a","e"};
string[] tablecol = new string[] { "Mansab","Ali","Aly","Ayaz" };
int i, j;
for (i = 0; i < tablerow.Length; i++)
{
RowDefinition gridrows = new RowDefinition();
gridrows.Height = new GridLength(66);
tablegrid.RowDefinitions.Insert(i, gridrows);
}
for (j = 0; j < tablecol.Length; j++)
{
ColumnDefinition gridCol = new ColumnDefinition();
tablegrid.ColumnDefinitions.Insert(j, gridCol);
}
// Setting background Image for all rows and columns Dynamically
int k = 0;
for (k = 0; k < tablecol.Length; k++)
{
Border brd = new Border();
brd.Height = 66;
brd.Width = 460;
brd.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
Grid.SetRow(brd, 0);
Grid.SetColumnSpan(brd, tablecol.Length);
tablegrid.Children.Add(brd);
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Resources/Images/top_bar_bg.png", UriKind.Relative));
brd.Background = brush;
}
int l;
for (l = 0; l < tablecol.Length; l++)
{
Border brd = new Border();
brd.Height = 66;
brd.Width = 460;
brd.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
Grid.SetRow(brd, 1);
Grid.SetColumnSpan(brd, tablecol.Length);
tablegrid.Children.Add(brd);
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Resources/Images/table_row_blue.png", UriKind.Relative));
brd.Background = brush;
}
ContentPanel.Children.Add(tablegrid);
}