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;
        }

 

posted @ 2018-01-12 21:30  <--青青子衿-->  阅读(585)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);