【问题标题】:How to color a particular row of a grid in c# using silverlight如何使用silverlight在c#中为网格的特定行着色
【发布时间】:2014-05-17 00:00:56
【问题描述】:

我正在研究 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 列)着色,那么我将如何做到这一点?

【问题讨论】:

  • 据我所知,Grid 面板不支持为特定的行/列着色。您将不得不尝试另一种方法。
  • 可以在同一行级别使用适当的列跨度将带有彩色填充/背景的矩形或边框插入并伪造它....但是为什么要在其中绘制网格我会问代码而不是 xaml。
  • @McGarnagle 你有什么建议?我的意思是我将在一行中有 1 个组合框一个文本框和一个文本块,我想更改每一行的颜色。
  • @ChrisW。是的,我可以只使用 c# 来设置带有颜色的边框或矩形,但该怎么做呢?
  • @user234839:我看到你的问题出现了很多,表明没有研究工作,更糟糕的是:现在有人多次暗示你使用xaml 解决方案,使用DataTemplates 而不是代码,但你拒绝再考虑。这整个方法散发着“只知道一把锤子”的味道。 Håkan Fahlstedt 为您提供了其中一个问题的an excelent answer,我强烈建议您阅读并了解如何使用DataTemplates

标签: c# .net silverlight grid silverlight-5.0


【解决方案1】:
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 );

【讨论】:

  • 感谢 ks 的回答,但我无法理解“Canvas.SetZOder(greenBackgroundBorder, -100);” ?并且“SetZOder”给出错误:“System.Windows.Controls.Canvas”不包含“SetZOder”的定义
  • @user234839:抱歉,刚刚发现有错别字。
  • 可以在这个边框里加文字吗?
  • @user234839:在同一个单元格中添加一个TextBlock
  • @user234839:是的,有效果。来自 Canvas 类的 AttachedProperty 在类外部产生效果有点违反直觉,但您可以在 Silverlight 基础库中的所有 Panels 中使用 ZOrder
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多