我这里所采用的方式是  DataGrid  是使用的列中的DataGridTextColumn  binding的参数,然后事件是在LoadingRow事件中,姜彦2018年1月12日 21:30

 

View代码

<DataGrid Grid.Row="1"  x:Name="dgFormula"  
                  AutoGenerateColumns="False" 
                  SelectionMode="Single"
                  AlternationCount="2"  
                  RowHeaderWidth="0" 
                  CanUserAddRows="False" 
                  VerticalAlignment="Top"
                  RowHeight="25"
                  FontSize="12"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  SelectionUnit="Cell"
                  Style="{StaticResource CommonDataGridStyle}"
                  ColumnHeaderStyle="{StaticResource CommonDataGridColumnHeaderStyle}"                 
                  RowStyle="{StaticResource CommonDataGridRowStyle}" LoadingRow="dgFormula_LoadingRow"
                  >

            <DataGrid.Columns>



                <DataGridTextColumn Header="" Width="35"
                                    Binding="{x:Null}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="公式号" Width="60" 
                                    Binding="{Binding Id}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="PId" Width="60" 
                                    Binding="{Binding PId}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="字符串PId" Width="60" 
                                    Binding="{Binding StrId}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="公式名称" Width="180" 
                                    Binding="{Binding FormulaName}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="脚本内容"  MinWidth="480" 
                                    Binding="{Binding ScriptContent}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="测试一下"  MinWidth="120" 
                                    Binding="{x:Null}" ClipboardContentBinding="{x:Null}"/>



            </DataGrid.Columns>
        </DataGrid>

 

 

 

//姜彦 20180112 21:23
        string strPId1 = string.Empty;  //全局变量  为了传递cell的变化内容,上一个跟下一个对比的传递媒介
        static  BrushConverter conv1 = new BrushConverter();
        Brush color0;//通过第三个值  交换颜色 原理 A B C 通过C 交换A B
        Brush color1= conv1.ConvertFromInvariantString("#E2E9E1") as Brush;//浅绿色EECEE0
        Brush color2 = conv1.ConvertFromInvariantString("#FFFFFF") as Brush;//白色
        private void dgFormula_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var obj1 = e.Row.DataContext;
            var vM = obj1 as TFormulaViewModel;
            if (strPId1 == vM.StrId)
            {
                e.Row.Background = (System.Windows.Media.Brush)color1;//浅绿色
            }
            else
            {
                e.Row.Background = (System.Windows.Media.Brush)color2;//白色   
                color0 = color1;//通过第三个值  交换颜色 原理 A B C 通过C 交换A B
                color1 = color2;
                color2 = color0;
            }
            strPId1 = vM.StrId;           

        }

 

公司项目中的代码编辑

 

View端没有做明显改动,只是增加了LoadingRow事件,对应的LoadingRow的代码如下(这里的heard是binding的  所以我换了一种思路,通过ViewModel中的变量做的对比,如下)

static BrushConverter conv1 = new BrushConverter();
        System.Windows.Media.Brush color0;
        System.Windows.Media.Brush color1 = conv1.ConvertFromInvariantString("#FFFFFF") as System.Windows.Media.Brush;//白色
        System.Windows.Media.Brush color2 = conv1.ConvertFromInvariantString("#E2E9E1") as System.Windows.Media.Brush;//浅绿色
        string sampleNo = string.Empty;

        private void grid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var obj1 = e.Row.DataContext;
            var vM1 = obj1 as ViewTestRequestInfo;
            if (sampleNo == vM1.SampleNo)
            {
                e.Row.Background = (System.Windows.Media.Brush)color1;
            }
            else
            {
                e.Row.Background = (System.Windows.Media.Brush)color2;
                color0 = color1;
                color1 = color2;
                color2 = color0;
            }
            sampleNo = vM1.SampleNo;

        }

 

 static BrushConverter conv1 = new BrushConverter();
        Brush color0;
        Brush color1 = conv1.ConvertFromInvariantString("#FFFFFF") as Brush;//白色
        Brush color2 = conv1.ConvertFromInvariantString("#E2E9E1") as Brush;//浅绿色
        string sampleNo = string.Empty;
        private void grid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
          
            var obj1 = e.Row.DataContext;
            var vM1 = obj1 as ViewTestRequestInfo;          

                 
            if (sampleNo==vM1.SampleNo)
            {
                e.Row.Background = (System.Windows.Media.Brush)color1;
            }
            else
            {
                e.Row.Background = (System.Windows.Media.Brush)color2;  
                color0 = color1;
                color1 = color2;
                color2 = color0;
            }
            sampleNo = vM1.SampleNo;
        }

 

相关文章:

  • 2021-05-23
  • 2021-12-10
  • 2021-12-26
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
相关资源
相似解决方案