1

I want to color the cells of my datagrid on dynamic conditions, and for now I just want all the cells red just to try.

With this code I am having that the variable "Row" is null. After reading numerous posts, I concluded that it is because the VirtualizingStackPanel.IsVirtualizingProperty is set to true to enhance performance. I understand that I need to turn this property off in the DataGrid "table" but for now with no success.

table.SetValue( VirtualizingStackPanel.IsVirtualizingProperty , false );
            table.ItemsSource = dt.DefaultView;
            for ( int r = 1 ; r < dt.Rows.Count ; r++ ) {
                for ( int c = 1 ; c < dt.Columns.Count ; c++ ) {
                    App.func.dmessage( "table.ItemContainerGenerator.Items.Count:" + table.ItemContainerGenerator.Items.Count );
                    DataGridRow Row = table.ItemContainerGenerator.ContainerFromIndex( r ) as DataGridRow;
                    DataGridCell cell = table.Columns[c].GetCellContent( Row ).Parent as DataGridCell;
                    //set background
                    cell.Background = Brushes.Red;
                }
            }

I have not seen any clear response to this question, so I'd really appreciate some clarity on how to set off this "VirtualizingStackPanel.IsVirtualizingProperty" property, so as to enable dynamic coloring of the table cells.

Context: the table is going to show electric node calculation voltages, and I want the users to set the upper and lower limits, and this cannot be done with XAML in design time.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user721807
  • 85
  • 1
  • 8
  • Would you mind telling us what other posts you tried? This seems very much like a duplicate of for instance this post http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values – Patrick Aug 07 '13 at 11:05
  • Sure: http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values http://stackoverflow.com/questions/16189717/how-to-change-single-cell-color-of-datagrid-in-wpf http://stackoverflow.com/questions/6713365/itemcontainergenerator-containerfromitem-returns-null http://stackoverflow.com/questions/165424/how-does-itemcontainergenerator-containerfromitem-work-with-a-grouped-list http://social.msdn.microsoft.com/Forums/vstudio/en-US/353819e9-3b98-4cb8-81b5-6fde398c5f63/itemcontainergeneratorcontainerfromindexi-returns-null- – user721807 Aug 07 '13 at 11:23
  • http://stackoverflow.com/questions/15977431/itemcontainergenerator-containerfromindex-returns-null-after-insert – user721807 Aug 07 '13 at 11:23
  • The fact that you use a virtualizingstackpanel should not affect the answer of the post I linked to. Why did that not work? Did you try to use another type of panel, such as a regular StackPanel? Also, when are you running the code in your question? – Patrick Aug 07 '13 at 11:44
  • How are you setting up `table`, is that done in XAML or also using code-behind? – Patrick Aug 07 '13 at 11:49
  • Why can't you set limits using XAML? It seems like that could easily be done using bindings to two different values? Would you mind explaining why it's not possible? – Patrick Aug 07 '13 at 11:50
  • Dear Patrick, perhaps I did not understand the post you linked but I feel it requires a certain previous table design. In my case the number of columns depend on the number of bus bars of a circuit, and the limits that will trigger the cell coloring are dynamic and comprise complex previous calculations, therefore my results table is nothing as simple as name|john. To my understanding XAML cannot be changed in run time, so any answer that uses XAML is not valid in my case, again, I found some valid approaches but they do not work, because the code "ROW" variable is NULL – user721807 Aug 07 '13 at 11:50
  • Is it possible to bind the grid with XAML to values that will only be generated in run time? – user721807 Aug 07 '13 at 12:01
  • Yes, you can use an ObservableCollection. The datagrid will add or remove rows when you use the collection. WPF would not be worth much if all it could display was predefined values at compile time. – Patrick Aug 07 '13 at 12:14
  • Regarding the other question, it binds to values, not necessarily a string. The ValueConverter uses the value that is passed to it (whatever that might be) and you can do any type of calculation on it that you need. So if you have a list of type `T`, that is the type you cast to in the valueconverter. – Patrick Aug 07 '13 at 12:16
  • I will try then and post the code if I succeed. Thank you. – user721807 Aug 07 '13 at 12:41

0 Answers0