Reputation:
I want to change the Foreground
of a item on a ListView
of my UWP.
I am using:
int i_DeleteRow = ListView1.SelectedIndex;
var item = ListView1.Items[i_DeleteRow] as ListViewItem;
if (item != null)
{
item.Foreground = new SolidColorBrush(Colors.Red);
}
But with this code item
is always null.
Any help is appreciated.
Upvotes: 2
Views: 1102
Reputation: 39976
You need to use ItemContainerGenerator.ContainerFromIndex
. It returns a DependencyObject
then you can cast it to ListBoxItem
and use the ListBoxItem
's properties like Foreground
:
ListViewItem item = (ListViewItem)(ListView1.ItemContainerGenerator.ContainerFromIndex(ListView1.SelectedIndex));
if (item != null)
{
item.Foreground = new SolidColorBrush(Colors.Red);
}
Upvotes: 2