如何更改数据网格中列的前景色 [英] How to change the fore color of column in data grid

查看:76
本文介绍了如何更改数据网格中列的前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在位置大于2时将原色从红色更改为绿色.
我的代码是
xaml

i want to change the forecolor from red to green when position is greater than 2..
my code is
xaml

<pre lang="xml"><my:DataGrid.Columns>
    <my:DataGridTemplateColumn Header="Icon" Width="50" IsReadOnly="True" >
        <my:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Image Source="{Binding Path=ImgPath}" Width="20" Height="20"/>
            </DataTemplate>
        </my:DataGridTemplateColumn.CellTemplate>
    </my:DataGridTemplateColumn>
    <my:DataGridTextColumn Header="Position"  Binding="{Binding Path=PO}"   />
    <my:DataGridTextColumn Header=" Name" Binding="{Binding Path=NA}"  />
    <my:DataGridTextColumn Header="Gender" Binding="{Binding Path=GE}"  />
     <my:DataGridTemplateColumn Header="Status" Width="50" IsReadOnly="True"  >
       <my:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Label Name="lbl" Content="{Binding Path=ST}" Foreground="Red" />
            </DataTemplate>
        </my:DataGridTemplateColumn.CellTemplate>
        </my:DataGridTemplateColumn>
        <my:DataGridTextColumn Header="Machine" Binding="{Binding Path=MA}"  />
</my:DataGrid.Columns>






xaml.cs






xaml.cs

dt = new DataTable();
           dt.Columns.Add("PO");
           dt.Columns.Add("NA");
           dt.Columns.Add("GE");
           dt.Columns.Add("ST");
           dt.Columns.Add("MA");
           dt.Columns.Add("ImgPath", typeof(BitmapImage));

           DataRow dr = dt.NewRow();
           dr[0] = "1";
           dr[1] = "SAM";
           dr[2] = "Male";

           dr[3] = "Offline";
           dr[4] = "Comp123";
           dr[5] = new BitmapImage(new Uri("/MTDLL_TEACHER;component/Images/offline.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);

           dr = dt.NewRow();
           dr[0] = "2";
           dr[1] = "JOY";
           dr[2] = "Male";

           dr[3] = "Online";

           dr[4] = "Comp124";
           dr[5] = new BitmapImage(new Uri("/MTDLL_TEACHER;component/Images/online.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);

           dr = dt.NewRow();
           dr[0] = "3";
           dr[1] = "ADITYA";
           dr[2] = "Male";
           dr[3] = "Online";
           dr[4] = "Comp124";
           dr[5] = new BitmapImage(new Uri("/MTDLL_TEACHER;component/Images/play.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);

           dr = dt.NewRow();
           dr[0] = "4";
           dr[1] = "VISHAL";
           dr[2] = "Male";
           dr[3] = "Offline";
           dr[4] = "Comp124";
           dr[5] = new BitmapImage(new Uri("/MTDLL_TEACHER;component/Images/offline.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);

           dr = dt.NewRow();
           dr[0] = "5";
           dr[1] = "ANAY";
           dr[2] = "Male";
           dr[3] = "Online";
           dr[4] = "Comp124";
           dr[5] = new BitmapImage(new Uri("/MTDLL_TEACHER;component/Images/online.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);

           dataGrid1.ItemsSource = dt.DefaultView;
           dataGrid1.SelectedValuePath = "NA";

           DataRow[] result = dt.Select("PO >= 2");
           // AND Sex = ''m''");
           foreach (DataRow row in result)
           {
           here i want to change the color from red
to green

           }

推荐答案

http://msdn.microsoft.com/en-us/library/z2akwyy7.aspx [ ^ ]

http://msdn.microsoft. com/en-us/library/system.windows.forms.datagrid.forecolor%28v = vs.71%29.aspx [ http://social.msdn.microsoft.com /Forums/zh-CN/winforms/thread/a104c6f9-9169-479b-81b0-f25ebb09a226/ [
http://msdn.microsoft.com/en-us/library/z2akwyy7.aspx[^]

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.forecolor%28v=vs.71%29.aspx[^]

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/a104c6f9-9169-479b-81b0-f25ebb09a226/[^]


这篇关于如何更改数据网格中列的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆