Note that there are some explanatory texts on larger screens.

plurals
  1. POIValueConverter Issue with WPF DataGridCells
    text
    copied!<p>I have a DataGrid that populates its data from a database via a DataSet. Now, what I need to do is color the background of the cells based on their value and a bunch of other factors. </p> <p>After spending the better part of two days scouring the internet, I have settled upon using an IValueConverter to perform this. After writing my code, the code executes fine; I have tested and have seen that 1) the IValueConverter class is being called and 2) it is indeed returning a Brush Color. </p> <p>My issue is that the cells are not being colored with the applied brush palette at run time. </p> <p>Oddly, I know that <i>something</i> is working because, when I select each row, the cells that are supposed to be colored are highlighted in a different color(in this case, a bright white) than the default "selection blue". So, for whatever reason, although the cells ARE converting AND calling the IValueConverter, the layout is <b>NOT</b> displaying it. I have also performed another test where I simply set the CellStyle to a straight color (in this case, LimeGreen) and ignored the IValueConverter completely, and the cells DID APPEAR in Lime-Green. This leads me to believe something is wrong somewhere between the Binding and the Convert(). </p> <p>I am absolutely baffled as to why the conversion color is not being displayed, and I've exhausted every search result in the first few pages of Google, so I am hoping someone here can take a look at my code and point out to me exactly why the DataGrid is acting this way.</p> <pre><code>DataTable dt = new DataTable(); dt = ((DataSet)ItemSource).Tables[0]; int counter = 0; foreach (DataColumn dc in dt.Columns) { switch(dc.ColumnName) { (....) case "SEAPCT": DataGridTextColumn col2 = new DataGridTextColumn(); col2.Header = (((DataSet)ItemSource).Tables[0].Columns[counter].ToString()).Replace("PCT", "%"); col2.Binding = new Binding(dc.ColumnName); Style style = new Style(typeof(DataGridCell)); Setter setter = new Setter(); setter.Property = DataGridCell.BackgroundProperty; Binding b = new Binding(dc.ColumnName); b.Converter = new PercentCellConverter(); setter.Value = b; style.Setters.Add(setter); col2.CellStyle = style; DashboardGrid.Columns.Insert(counter, col2); counter++; break; } } DashboardGrid.ItemsSource = dt.DefaultView; } </code></pre> <p>And, for reference, my IValueConverter class:</p> <pre><code>public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string n = value.ToString(); System.Diagnostics.Debug.WriteLine(n); if (double.Parse(n) &gt; 0) return Brushes.LimeGreen; else if (double.Parse(n) == 0) return Brushes.Yellow; else return Brushes.Red; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload