0

I want to change the color of a ListViewItem in a ListView by it´s index. But when I do this, I get a NullReferenceException:

Brush brush = new SolidColorBrush(Windows.UI.Colors.Green);
(baustellenListView.Items.ElementAt(i) as ListViewItem).Foreground = brush;

baustellenListView is the name of the ListView.

Thank you for help!

zx485
  • 24,099
  • 26
  • 45
  • 52
omginput
  • 69
  • 7
  • 2
    Did you debug _what_ is `null`? Is it the `baustellenListView`? Or does `ElementAt(i)` return `null` or something that is not a `ListViewItem` (so `as` returns `null`)? – René Vogt Feb 07 '17 at 15:02
  • + Windows.UI.Colors.Green {#FF008000} Windows.UI.Color + baustellenListView.Items {Windows.UI.Xaml.Controls.ItemCollection} Windows.UI.Xaml.Controls.ItemCollection + brush {Windows.UI.Xaml.Media.SolidColorBrush} Windows.UI.Xaml.Media.Brush {Windows.UI.Xaml.Media.SolidColorBrush} i 1 int + this {SchlachterFliesen.Baustelle} SchlachterFliesen.Baustelle – omginput Feb 07 '17 at 15:16
  • i have no idea what should be null here – omginput Feb 07 '17 at 15:16
  • baustellenListView is definitely not null and all items are ListViewItems – omginput Feb 07 '17 at 15:17
  • lol, this is no duplicate. This is a special case -.- – omginput Feb 07 '17 at 15:20
  • There are very rarely really special cases to that. The ways how to debug and investigate the reasons for a `NullReferenceException` are all explained in the linked question. And there is nothing else we/you can do except investigating. From the code you posted, there are only the options I mentioned above. I really think `baustellenListView.Items` does contain the data objects, not `ListViewItem` instances. So the `as` operator returns `null`. Please _debug_ and check the type of objects in `baustellenListView.Items`. – René Vogt Feb 07 '17 at 15:23
  • Ok, it´s the data objects. But how do I get the ListViewItems itself? – omginput Feb 07 '17 at 15:33
  • Oh, wir können auch Deutsch schreiben. :) – omginput Feb 07 '17 at 15:42
  • Also Frage ist eigentlich wie komme ich an die ListViewItem Objekte aus der ListView ran. Items gibt nur die Daten Objekte. – omginput Feb 07 '17 at 15:45
  • It seems this question is really "How do I get an item in a list and change it's color?" Just because the way they've tried resulted in a NullReferenceException and explaining what that is will only show the problem with the OPs code, not how to solve their actual problem. – Matt Lacey Feb 07 '17 at 15:49
  • Yes. My question is indeed how to get an Item out of the ListView with an index and change its color. With ListView.Items I only get the data objects not the items. Could anyone please tell me how to get the actual items? Thank you so much – omginput Feb 07 '17 at 15:56

0 Answers0