none
DataGridRow 数据和对应行问题 RRS feed

  • 问题

  • 以下代码数据对应的行有问题。我想特殊处理第一行,将背景色变黄,但是处理的效果第二行变黄,第一行显示成了 最后一行的对应效果(好像每行都被往下推了一行),最后一行的效果出现在第一行。数据没有问题只是显示的背景色有问题。

                <data:DataGrid x:Name="cbChart" LoadingRow="MyDG_LoadingRow">
                       
            </data:DataGrid>

    protected void MyDG_LoadingRow(object sender, DataGridRowEventArgs e)
            {

               
                int rowIndexs = e.Row.GetIndex();

                int columnindex = 22;
                int maxValue = 300;
                int minValue = 0;
              
                Setter green = new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.Green));
                Style stylegreen = new Style(typeof(DataGridCell));
                stylegreen.Setters.Add(green);


                Setter yellow = new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
                Style styleyellow = new Style(typeof(DataGridCell));
                styleyellow.Setters.Add(yellow);


                Setter black = new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.Black));
                Style styleblack = new Style(typeof(DataGridCell));
                styleblack.Setters.Add(black);

                ServiceReference1.MFDemoData st = e.Row.DataContext as ServiceReference1.MFDemoData;

     

                if (rowIndexs == 0)
                {
                    for (int i = 1; i < columnindex; i++)
                    {
                        cbChart.Columns[i].CellStyle = styleyellow;
                    }
                    return;
                }
                else if (rowIndexs < 18)
                {
                   
                    Setter red0 = new Setter(DataGridCell.BackgroundProperty, bindcoller(st.Col0, minValue, maxValue));
                    Style stylered0 = new Style(typeof(DataGridCell));
                    stylered0.Setters.Add(red0);
                    cbChart.Columns[1].CellStyle = stylered0;

                    Setter red140 = new Setter(DataGridCell.BackgroundProperty, bindcoller(st.Col140, minValue, maxValue));
                    Style stylered140 = new Style(typeof(DataGridCell));
                    stylered140.Setters.Add(red140);
                    cbChart.Columns[2].CellStyle = stylered140;

                    Setter red147 = new Setter(DataGridCell.BackgroundProperty, bindcoller(st.Col147, minValue, maxValue));
                    Style stylered147 = new Style(typeof(DataGridCell));
                    stylered147.Setters.Add(red147);
                    cbChart.Columns[3].CellStyle = stylered147;

                    Setter red156 = new Setter(DataGridCell.BackgroundProperty, bindcoller(st.Col156, minValue, maxValue));
                    Style stylered156 = new Style(typeof(DataGridCell));
                    stylered156.Setters.Add(red156);
                    cbChart.Columns[4].CellStyle = stylered156;

                }

    }

     private string bindcoller(string value, int minValue, int maxValue)
            {
                Color cr = new Color();
                int val = int.Parse(value);
                if (val < minValue || val > maxValue)
                {
                    return "#FFEA1111";
                }
                string coller = null;
                if (val < 1)
                {
                    coller = "#FFEA1111";
                }
                else
                {

                    int tempRR = 255 - (val - minValue) * (255 - 99) / (maxValue - minValue);
                    int tempGG = 190 - (val - minValue) * (239 - 190) / (maxValue - minValue);
                    int tempBB = 123 - (val - minValue) * (156 - 123) / (maxValue - minValue);
                    coller = "RGB(" + tempRR.ToString() + "," + tempGG.ToString() + "," + tempBB.ToString() + ")";
                   
                    cr.A = 255;
                    cr.R = (Byte)tempRR;
                    cr.G = (Byte)tempGG;
                    cr.B = (Byte)tempBB;
                }
                return cr.ToString();
            }

    2010年3月22日 5:51

答案

  • 原来每次都要从新绑定~

     DataGridColumn column1 = this.cbChart.Columns[1];
                    FrameworkElement fe1 = column1.GetCellContent(e.Row);
                    FrameworkElement result1 = GetParent(fe1, typeof(DataGridCell));
                    if (result1 != null)
                    {
                        DataGridCell cell1 = (DataGridCell)result1;
                        cell1.Background = new SolidColorBrush(bindcoller(st.Col0, minValue, maxValue));
                    }

    • 已标记为答案 小达 2010年3月24日 7:25
    2010年3月24日 7:25

全部回复

  • 有高手 知道吗?

    2010年3月23日 6:49
  • 原来每次都要从新绑定~

     DataGridColumn column1 = this.cbChart.Columns[1];
                    FrameworkElement fe1 = column1.GetCellContent(e.Row);
                    FrameworkElement result1 = GetParent(fe1, typeof(DataGridCell));
                    if (result1 != null)
                    {
                        DataGridCell cell1 = (DataGridCell)result1;
                        cell1.Background = new SolidColorBrush(bindcoller(st.Col0, minValue, maxValue));
                    }

    • 已标记为答案 小达 2010年3月24日 7:25
    2010年3月24日 7:25