>
gridview column background color code
Welcome, Guest

by dhanendra on Saturday, 19 September 2009
gridview column background color code


This example has two colored columns:

1) first column has Biege color, wich assigned from C# code-behind;
2) fird column has Gold color, wich assigned from XAML markup.

XAML

<Window x:Class="Test.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:w="clr-namespace:System.Windows.Workarounds" 
    Title="GridView Column Coloring Example" Height="300" Width="300">  
    <ListView ItemsSource="{Binding}" Name="PeopleListView">  
        <ListView.ItemContainerStyle> 
            <Style TargetType="{x:Type ListViewItem}">  
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>  
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>  
                <Setter Property="Margin" Value="0,-2,0,-2"/>  
                <Setter Property="Padding" Value="0,2,0,2"/>  
            </Style> 
        </ListView.ItemContainerStyle> 
        <ListView.View> 
            <GridView> 
                <GridViewColumn Header="First Name">  
                    <GridViewColumn.CellTemplate> 
                        <DataTemplate> 
                            <Grid Background="{Binding View.Columns[0].Background, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}">  
                                <TextBlock Text="{Binding FirstName}" /> 
                            </Grid> 
                        </DataTemplate> 
                    </GridViewColumn.CellTemplate> 
                </GridViewColumn> 
                <GridViewColumn Header="Middle Name">  
                    <GridViewColumn.CellTemplate> 
                        <DataTemplate> 
                            <Grid Background="{Binding View.Columns[1].Background, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}">  
                                <TextBlock Text="{Binding MiddleName}" /> 
                            </Grid> 
                        </DataTemplate> 
                    </GridViewColumn.CellTemplate> 
                </GridViewColumn> 
                <GridViewColumn Header="Last Name" w:GridViewColumn.Background="Gold">  
                    <GridViewColumn.CellTemplate> 
                        <DataTemplate> 
                            <Grid Background="{Binding View.Columns[2].Background, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}">  
                                <TextBlock Text="{Binding LastName}" /> 
                            </Grid> 
                        </DataTemplate> 
                    </GridViewColumn.CellTemplate> 
                </GridViewColumn> 
            </GridView> 
        </ListView.View> 
    </ListView> 
</Window> 
 


C#

using System;  
using System.Collections.Generic;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Media;  
 
namespace Test  
{  
 
    public partial class Window1 : Window  
    {  
 
        public Window1()  
        {  
 
            InitializeComponent();  
 
            List<Person> Items = new List<Person>();  
 
            Items.Add(new Person("Mike""W""Edwards"));  
            Items.Add(new Person("Jane""S""Smith"));  
            Items.Add(new Person("Thomas""A""Jenkins"));  
 
            DataContext = Items;  
 
            GridViewColumn FirstNameColumn = ((GridView)PeopleListView.View).Columns[0];  
            System.Windows.Workarounds.GridViewColumn.SetBackground(FirstNameColumn, Brushes.Beige);  
 
        }  
 
    }  
 
    public class Person  
    {  
        public string FirstName { getset; }  
        public string MiddleName { getset; }  
        public string LastName { getset; }  
        public Person(string firstName, string middleName, string lastName)  
        {  
            FirstName = firstName;  
            MiddleName = middleName;  
            LastName = lastName;  
        }  
    }  
 
}  
 
namespace System.Windows.Workarounds  
{  
 
    public static class GridViewColumn  
    {  
 
        public static readonly DependencyProperty BackgroundProperty =  
            DependencyProperty.RegisterAttached("Background"typeof(Brush),  
            typeof(System.Windows.Controls.GridViewColumn), new PropertyMetadata(Brushes.Transparent));  
 
        public static Brush GetBackground(System.Windows.Controls.GridViewColumn column)  
        {  
            return (Brush)column.GetValue(BackgroundProperty);  
        }  
 
        public static void SetBackground(System.Windows.Controls.GridViewColumn column, Brush value)  
        {  
            column.SetValue(BackgroundProperty, value);  
        }  
 
    }  
 



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering