WPF DataGrid 根据某一特定内容进行交叉变色

我这里所采用的方式是  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;
        }

 

转载于:https://www.cnblogs.com/jiangyan219/articles/8277776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值