Solved

Wpf  Datagrid  Row Colors

Posted on 2015-02-14
3
233 Views
Last Modified: 2015-03-27
As I  am  going to  develop  a  new  project  by  extending  capabilities  of  previous  project which was  developed  in WPF,VC#. My  new  project  is  going  to  communicate  with  two  different  biomedical  instruments. From  instrument  I  will  read  results  and  save into database. From database  by  processing  results  I  will show  it  in different  formats  on  various forms as  per  domain  requirement. While  saving  results  in database  I  will  store  it   with  instrument  id.
Now  while  showing on  datagrid  I  have  to  display  results  in different  ForeColor  and  different  backcolor  of rows for two  different  instrument.I  have  searched  in  google   and I  have  just  known  that  coloring  rows  is possible  in  WPF. Now  Please  by  understanding  my  problem,can  any one  tell  me  syntax  or  tips. I  am  right  now  in  design  phase  for  my  project. So  tips  from  experts  will be  helpful  at  this  time. I  want  to  have  good  graphs  for  displaying  on  WPF,Is  there  any  good  cheap  resource  available  in  market? Please  suggest
0
Comment
Question by:enthurao
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 40612140
You can set the AlternatingCount to 2, and then add a row style like this:

<DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Style.Triggers>
                    <Trigger Property="AlternationIndex" Value="0">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                    <Trigger Property="AlternationIndex" Value="1">
                        <Setter Property="Background" Value="SkyBlue" />
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=Selectable}" Value="False">
                        <DataTrigger.Setters>
                            <Setter Property="Background" Value="Gainsboro" />
                        </DataTrigger.Setters>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

Open in new window

0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 250 total points
ID: 40612483
There are several different ways to achieve that

1 - Use DataTrigger inside a DataTemplate to alter color, e.g. (especially useful when you already have a DataTemplate)

<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=ItemType}" Value="Something">
<Setter Property="ListBoxItem.Foreground" Value="Red"></Setter>
</DataTrigger>
</DataTemplate.Triggers>

Open in new window


In your case, you may have to write a value converter to return the type of the Item (say its className) and use that as trigger value.

2 - DataTrigger can also be used inside a Style (as already shown).

3 - Use a StyleSelector that lets you change the entire Style of an item, without having to write a template

4 - Use different DataTemplates either declaratively or using TemplateSelector.
0
 

Author Comment

by:enthurao
ID: 40627403
Please  give  me  some  time  to  implement  this,I  am  very  busy  with  some other  task
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now