Click here to Skip to main content
15,888,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello!

I have a question.

how to make highlight selection of row in List View by different colors? but I want to do it correct with list selectors.

Please provide some simple for few colors

thank you
Posted

 
Share this answer
 
hello thomas,

I think We Can Easily do this by using WPF Application:

the complete solution is below:

for Start VS2010 then Add a new project named as <wpf_datagrid_propertytriggers> using a WPF Application:
then Add This Code in MainWindow.xmal

---
<br />
<Window x:Class="WPF_DataGrid_PropertyTriggers.MainWindow"<br />
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
        xmlns:src="clr-namespace:WPF_DataGrid_PropertyTriggers"<br />
        Title="MainWindow" Height="382" Width="892"><br />
    <Window.Resources><br />
        <src:EmployeeCollection x:Key="EmpCol"></src:EmployeeCollection><br />
        <Style TargetType="{x:Type DataGridCell}"><br />
            <Style.Triggers><br />
                <DataTrigger Binding="{Binding Designation}" Value="Manager"><br />
                    <Setter Property="Background" Value="Green"></Setter><br />
                </DataTrigger><br />
                <DataTrigger Binding="{Binding Designation}" Value="Clerk"><br />
                    <Setter Property="Background" Value="Red"></Setter><br />
                </DataTrigger><br />
                <DataTrigger Binding="{Binding Designation}" Value="Supervisor"><br />
                    <Setter Property="Background" Value="Chocolate"></Setter><br />
                </DataTrigger><br />
                <DataTrigger Binding="{Binding Designation}" Value="Operator"><br />
                    <Setter Property="Background" Value="Magenta"></Setter><br />
                </DataTrigger><br />
            </Style.Triggers><br />
        </Style><br />
    </Window.Resources><br />
    <Grid DataContext="{Binding Source={StaticResource EmpCol}}"><br />
        <DataGrid AutoGenerateColumns="True" Height="272" HorizontalAlignment="Left" Margin="43,48,0,0"<br />
                  Name="dgEmp" VerticalAlignment="Top" Width="652" ItemsSource="{Binding}"<br />
                   ColumnWidth="*"/><br />
        <TextBlock Height="45" HorizontalAlignment="Left" Margin="132,0,0,0"<br />
                   Name="textBlock1" Text="Employee Infoemation System"<br />
                   VerticalAlignment="Top" Width="467" OpacityMask="#FF930000"<br />
                   FontStyle="Oblique" FontWeight="ExtraBold" FontSize="26" TextWrapping="WrapWithOverflow"<br />
                   TextTrimming="CharacterEllipsis" /><br />
        <StackPanel Height="250" HorizontalAlignment="Left" Margin="717,52,0,0" Name="stackPanel1"<br />
                    VerticalAlignment="Top" Width="125"><br />
            <StackPanel Orientation="Horizontal"><br />
                <TextBlock Width="80" Text="Manager" FontSize="15"></TextBlock><br />
                <Rectangle Fill="Red" Width="44"></Rectangle><br />
            </StackPanel><br />
            <StackPanel Orientation="Horizontal"><br />
                <TextBlock Width="80" Text="Clerk" FontSize="15"></TextBlock><br />
                <Rectangle Fill="Yellow" Width="44"></Rectangle><br />
            </StackPanel><br />
            <StackPanel Orientation="Horizontal"><br />
                <TextBlock Width="80" Text="Supervisor" FontSize="15"></TextBlock><br />
                <Rectangle Fill="Chocolate" Width="44"></Rectangle><br />
            </StackPanel><br />
            <StackPanel Orientation="Horizontal"><br />
                <TextBlock Width="80" Text="Operator" FontSize="15"></TextBlock><br />
                <Rectangle Fill="Magenta" Width="44"></Rectangle><br />
            </StackPanel><br />
        </StackPanel><br />
        <TextBlock Height="35" HorizontalAlignment="Left" Margin="717,11,0,0" Name="textBlock2" Text="Legend" VerticalAlignment="Top" Width="123" FontSize="20" /><br />
    </Grid><br />
</Window><br />
<br />

--------------------------------
Now Add A class(.cs file) names As <datafiles.cs>
and Add this below code in that file
--
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Collections.ObjectModel;<br />
<br />
namespace WPF_DataGrid_PropertyTriggers<br />
{<br />
    public class Employee<br />
    {<br />
        public int EmpNo { get; set; }<br />
        public string EmpName { get; set; }<br />
        public string DeptName { get; set; }<br />
        public string Designation { get; set; }<br />
        public int Experience { get; set; }<br />
        public int Salary { get; set; }<br />
    }<br />
<br />
    public class EmployeeCollection : ObservableCollection<Employee><br />
    {<br />
<br />
        public EmployeeCollection()<br />
        {<br />
<br />
            Add(new Employee() { EmpNo = 101, EmpName = "Ramesh", DeptName = "IT", Designation = "Manager", Experience = 15, Salary = 300000 });<br />
            Add(new Employee() { EmpNo = 102, EmpName = "Ajay", DeptName = "Accts", Designation = "Clerk", Experience = 12, Salary = 200000 });<br />
            Add(new Employee() { EmpNo = 103, EmpName = "Atul", DeptName = "Tech", Designation = "Supervisor", Experience = 10, Salary = 400000 });<br />
            Add(new Employee() { EmpNo = 104, EmpName = "Narayan", DeptName = "Hrd", Designation = "Supervisor", Experience = 14, Salary = 400000 });<br />
            Add(new Employee() { EmpNo = 105, EmpName = "Baban", DeptName = "Hrd", Designation = "Manager", Experience = 18, Salary = 300000 });<br />
            Add(new Employee() { EmpNo = 106, EmpName = "Mandar", DeptName = "IT", Designation = "Operator", Experience = 12, Salary = 200000 });<br />
            Add(new Employee() { EmpNo = 107, EmpName = "Suraj", DeptName = "Accts", Designation = "Operator", Experience = 22, Salary = 200000 });<br />
            Add(new Employee() { EmpNo = 108, EmpName = "Chaitanya", DeptName = "Tech", Designation = "Supervisor", Experience = 13, Salary = 400000 });<br />
            Add(new Employee() { EmpNo = 109, EmpName = "Anand", DeptName = "Tech", Designation = "Manager", Experience = 16, Salary = 300000 });<br />
            Add(new Employee() { EmpNo = 110, EmpName = "Kapil", DeptName = "Hrd", Designation = "Clerk", Experience = 10, Salary = 200000 });<br />
        }<br />
    }<br />
}<br />
<br />
<br />

--
After This Start Debugging your project.
you will get the window with different colors rows on role basis..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900