Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Hello every one.
By searching I've done following code.
 
I'm facing one problem regarding changing color of particular cell of a list view by comparing its value present in the tooltip in WPF.
 
Following is the XAML code
<ListView Name="lstData">
     <ListView.View>
         <GridView/>
     </ListView.View>
</ListView>
 
And following is the code behind
public partial class DataDispay : UserControl
    {
        DataTable dataTable = new DataTable();
        GridView gv = null;
        public DataDispay()
        {
            InitializeComponent();
            gv = (GridView)lstData.View;
            lstData.ItemsSource = ((System.ComponentModel.IListSource)dataTable).GetList();
        }
 
        public void setDataTable(DataRow[] dr_Value, DataRow[] dr_Header)
        {
            int srNo = 0;
            if (dr_Value.Length > 0)
            {
                try
                {
                    dataTable.Rows.Clear();
                    dataTable.Columns.Clear();
                    dataTable.Clear();
 
                    //add columns parameter data
                    foreach (DataRow dr in dr_Value)
                    {
                        srNo++;
                        if (srNo == 1)
                        {
                            dataTable.Columns.Add(new DataColumn("SrNo"));
                            foreach (DataColumn dc in dr.Table.Columns)
                            {
                                if (dc.ColumnName.Contains("ResultStatus"))
                                {
 
                                }
                                else
                                {
                                    dataTable.Columns.Add(new DataColumn(dc.ColumnName));
                                }
 
                            }
                        }
                    }
                    DataRow newRow_Nominal = dataTable.NewRow();
                    DataRow newRow_USL = dataTable.NewRow();
                    DataRow newRow_LSL = dataTable.NewRow();
                    foreach (DataRow dr in dr_Header)
                    {
                        newRow_Nominal[dr["ReportCharName"].ToString()] = dr["NominalSize"];
                        newRow_USL[dr["ReportCharName"].ToString()] = dr["USL"];
                        newRow_LSL[dr["ReportCharName"].ToString()] = dr["LSL"];
                    }
                    newRow_Nominal["CompSrNo"] = "NominalSize";
                    newRow_USL["CompSrNo"] = "USL";
                    newRow_LSL["CompSrNo"] = "LSL";
 
                    dataTable.Rows.Add(newRow_Nominal);
                    dataTable.Rows.Add(newRow_USL);
                    dataTable.Rows.Add(newRow_LSL);
                    srNo= 0;
                    foreach (DataRow dr in dr_Value)
                    {
                        srNo++;
                        DataRow newRow = dataTable.NewRow();
                        foreach (DataColumn dc in dr.Table.Columns)
                        {
                            if (!dc.ColumnName.Contains("ResultStatus"))
                            {
                                newRow[dc.ColumnName] = dr[dc.ColumnName];
                            }
                        }
                        newRow["SrNo"] = srNo;
                        dataTable.Rows.Add(newRow);
                    }
 
                    gv.Columns.Clear();
                    foreach (DataColumn col in dataTable.Columns)
                    {
                        GridViewColumn gvCol = new GridViewColumn();
                        gvCol.Header = col.ColumnName;
                        Binding bind = new Binding(col.ColumnName);
                        
                        DataTemplate newDataTemplete = new DataTemplate();
                        
                        FrameworkElementFactory notes = new FrameworkElementFactory(typeof(TextBlock));
                        notes.SetBinding(TextBlock.TextProperty, bind);
                        notes.SetValue(TextBlock.ToolTipProperty, col.ColumnName);
                        StackPanel sp = new StackPanel();
                        sp.Children.Add(new TextBlock() { Text = "USL = " + newRow_USL[col].ToString(), Name = "txtUSL", Tag = newRow_USL[col].ToString() });
                        sp.Children.Add(new TextBlock() { Text = "Nominal = " + newRow_Nominal[col].ToString(), Name = "txtNominal", Tag = newRow_Nominal[col].ToString() });
                        sp.Children.Add(new TextBlock() { Text = "LSL = " + newRow_LSL[col].ToString(), Name = "txtLSL", Tag = newRow_LSL[col].ToString() });
                        
                        notes.SetValue(TextBlock.ToolTipProperty, sp);
                        if (col.ColumnName.Equals("SrNo"))//this will change whole column color
                        {
                            notes.SetValue(TextBlock.ForegroundProperty, Brushes.Red);
                        }
                        else
                        {
                            notes.SetValue(TextBlock.ForegroundProperty, Brushes.Blue);
                        }
                        newDataTemplete.VisualTree = notes;
 
                        gvCol.CellTemplate = newDataTemplete;
 
                        gv.Columns.Add(gvCol);
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\n\nError trace:\n" + ex.StackTrace, System.Windows.Forms.Application.ProductName);
                }
            }
        }
    }
 

I want to change the color of cell if the tool tip TextBox's tag having double value and by comparing the USL and LSL TextBox Tag value.
 
If you have not understood what I'm trying to ask. Please reply for the same.
 
Please give me some solution for this.
 
Thanks in advance.
Posted 11-Jan-13 20:02pm
Edited 12-Jan-13 2:08am
v4

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100