Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
XML
<ListView Name="lstData">
     <ListView.View>
         <GridView/>
     </ListView.View>
</ListView>


And following is the code behind
C#
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
Updated 12-Jan-13 1:08am
v4

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