如何使用silverlight在c#中为网格的特定行着色

如何使用silverlight在c#中为网格的特定行着色,c#,.net,silverlight,grid,silverlight-5.0,C#,.net,Silverlight,Grid,Silverlight 5.0,我正在制作c#silverlight。我必须为使用c#创建的特定列涂上颜色(绿色) 我有6行3列的网格,如下所示: Grid myGrid = new Grid(); myGrid.Width = 350; myGrid.Height = 280; myGrid.HorizontalAlignment = HorizontalAlignment.Left; myGrid.VerticalAlignm

我正在制作c#silverlight。我必须为使用c#创建的特定列涂上颜色(绿色)

我有6行3列的网格,如下所示:

 Grid myGrid = new Grid();
            myGrid.Width = 350;
            myGrid.Height = 280;
            myGrid.HorizontalAlignment = HorizontalAlignment.Left;
            myGrid.VerticalAlignment = VerticalAlignment.Top;
            myGrid.ShowGridLines = false;
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();
            myGrid.ColumnDefinitions.Add(colDef1);
            myGrid.ColumnDefinitions.Add(colDef2);
            myGrid.ColumnDefinitions.Add(colDef3);
            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();
            RowDefinition rowDef4 = new RowDefinition();
            RowDefinition rowDef5 = new RowDefinition();
            RowDefinition rowDef6 = new RowDefinition();

            myGrid.RowDefinitions.Add(rowDef1);
            myGrid.RowDefinitions.Add(rowDef2);
            myGrid.RowDefinitions.Add(rowDef3);
            myGrid.RowDefinitions.Add(rowDef4);
            myGrid.RowDefinitions.Add(rowDef5);
            myGrid.RowDefinitions.Add(rowDef6);

现在如果我必须给这个网格的第二整行(我的意思是在这一行的3列中)上色,那么我将如何做?

据我所知,
网格
面板不支持为特定行/列上色。您必须尝试另一种方法。只需在同一行级别上用适当的列间距塞入一个带有彩色填充/背景的矩形或边框,然后将其伪造……但我会问您为什么要在代码中绘制网格而不是xaml。@McGarnagle您有什么建议吗?我的意思是,我将有一个组合框、一个文本框和一个文本块,我想更改每行的颜色。@ChrisW。是的,我可以只使用c#来创建带有颜色的边框或矩形,但如何做到这一点?@user234839:我看到你的问题大量出现,没有显示出任何研究成果,更糟糕的是:现在有几次有人暗示你使用
xaml
解决方案,使用
DataTemplates
而不是代码,但您拒绝重新考虑。整个方法散发着“只知道一把锤子”的味道。Håkan Fahlstedt向您提出了其中一个问题,我强烈建议您阅读并理解如何使用
DataTemplates
.than ks作为答案,但我无法理解“Canvas.SetZOder(greenBackgroundBorder,-100);”?“SetZOder”给出错误:“System.Windows.Controls.Canvas”不包含“SetZOder”的定义@user234839:抱歉,刚刚注意到它包含一个打字错误。是否可以在此边框中添加文本?@user234839:向该边框添加一个文本块cell@user234839:是的,它有效果。有些违反直觉的是,来自
画布
类的
AttachedProperty
在类外具有效果,但您可以在Silverlight基本库的所有
面板
中使用
ZOrder
var greenBackgroundBorder = new Border(){
    Background=new SolidColorBrush(Colors.Green)};
myGrid.Children.Add(greenBackgroundBorder);

// stay always behind other elements
Canvas.SetZOder(greenBackgroundBorder, -100);

//entire second row
Grid.SetColumnSpan(greenBackgroundBorder,3);
Grid.SetRow(greenBackgroundBorder, 1 );