改變DataGrid某一行和單元格的顏色

前段時間作WPF項目,須要改變DataGrid某一行的顏色、高度,以及某個單元格的顏色、單元格字體的顏色,天然就必需取到datagrid的一行和一行的單元格,網上也是搜索了很久才找到,記錄下來便於使用。ide

 

一、前臺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

 

二、建立一個數據源並綁定,此處是建立一個datatablethis

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

 

三、獲取單行spa

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

 

四、獲取單元格code

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

 

五、若是在項目中把建立數據源、綁定數據源、對datagrid進行操做(改變行的顏色、高度)都寫在一個事件中,其中在取datagrid的row時出現錯誤:未將對象引用設置到對象的實例。對象

解決的方法:blog

   //建立數據源、綁定數據源

if (!Window.GetWindow(dataGrid1).IsVisible) { Window.GetWindow(dataGrid1).Show(); } dataGrid1.UpdateLayout();//能夠獲取某一行、某一行的單元格
相關文章
相關標籤/搜索