首页 > 学院 > 开发设计 > 正文

改变DataGrid某一行和单元格的颜色

2019-11-14 16:27:27
字体:
来源:转载
供稿:网友

前段时间做WPF项目,需要改变DataGrid某一行的颜色、高度,以及某个单元格的颜色、单元格字体的颜色,自然就必需取到datagrid的一行和一行的单元格,网上也是搜索了好久才找到,记录下来便于使用。

 

1、前台WPF界面添加一个DataGrid控件,并添加两列(便于编写,达到目的即可)

<DataGrid AutoGenerateColumns="False" Height="642" HorizontalAlignment="Left" Margin="131,57,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="799" CanUserAddRows="True" LoadingRow="dataGrid1_LoadingRow" GridLinesVisibility="None">            <DataGrid.ColumnHeaderStyle >                <Style TargetType="DataGridColumnHeader">                    <Setter PRoperty="Height" Value="50"></Setter>                </Style>            </DataGrid.ColumnHeaderStyle>            <DataGrid.Columns>                <DataGridTextColumn Header="id" Binding="{Binding Path=id}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>                <DataGridTextColumn Header="name" Binding="{Binding Path=name}" ElementStyle="{StaticResource dgCell}"></DataGridTextColumn>            </DataGrid.Columns>        </DataGrid>
View Code

 

2、创建一个数据源并绑定,此处是创建一个datatable

DataTable dt = new DataTable();            dt.Columns.Add(new DataColumn("id", typeof(int)));            dt.Columns.Add(new DataColumn("name", typeof(string)));            for (int i = 0; i < 6; i++)            {                DataRow dr = dt.NewRow();                if (i == 3)                {                    dr["id"] = DBNull.Value;                    dr["name"] = DBNull .Value ;                    dt.Rows.Add(dr);                }                else                {                    dr["id"] = i;                    dr["name"] = "tom" + i.ToString();                    dt.Rows.Add(dr);                }            }            this.dataGrid1.CanUserAddRows = false;            this.dataGrid1.ItemsSource = dt.DefaultView;
View Code

 

3、获取单行

for (int i = 0; i < this.dataGrid1.Items.Count; i++)            {                DataRowView drv = dataGrid1.Items[i] as DataRowView;                DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);                if (i == 2)                {                    row.Height = 50;                    row.Background = new SolidColorBrush(Colors.Blue);                    drv["id"] = 333;                }                if (drv["id"] == DBNull.Value)                {                    row.Background = new SolidColorBrush(Colors.Green);                    row.Height = 8;                }            }

 

4、获取单元格

for (int i = 0; i < this.dataGrid1.Items.Count; i++)            {                DataRowView drv = dataGrid1.Items[i] as DataRowView;                DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);
         
if (i == 4) { DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(1); cell.Background = new SolidColorBrush(Colors.Red); } }

public static T GetVisualChild<T>(Visual parent) where T : Visual { T childContent = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); childContent = v as T; if (childContent == null) { childContent = GetVisualChild<T>(v); } if (childContent != null) { break; } } return childContent; }

 

5、如果在项目中把创建数据源、绑定数据源、对datagrid进行操作(改变行的颜色、高度)都写在一个事件中,其中在取datagrid的row时出现错误:未将对象引用设置到对象的实例。

解决的方法:

   //创建数据源、绑定数据源

if (!Window.GetWindow(dataGrid1).IsVisible) { Window.GetWindow(dataGrid1).Show(); } dataGrid1.UpdateLayout();

//可以获取某一行、某一行的单元格

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表