I am using @Romasz 's answer from Alternating Colors of rows in ListView in Windows Phone 8.1 to give alternate background to ListView
items. I change it in order to highlight the selected item, like this:
<local:AlternateConverter CurrentList="{Binding ElementName=myList, Path=ItemsSource}"
HighlightIndex="{Binding ElementName=myList, Path=SelectedIndex}"
x:Key="AlternateConverter"/>
Note: I removed the AlternateBrushes
property as I only need static colors and add in HighlightItem
property binding to the list's selected index.
Then I changed the converter class AlternateConverter
appropriately as follow: [NB: this is C++ counterpart.]
Object^ AlternateConverter::Convert(Object^ value, TypeName targetType, Object^ parameter, String^ language)
{
auto list = reinterpret_cast<IVector<Object^>^>(CurrentList);
unsigned int i;
bool r = list->IndexOf(value, &i);
// This is added to highlight the selected item with a different color
if (i == HighlightIndex)
return BrushHighlight;
return i % 2 == 0 ? BrushEven : BrushOdd;
}
The problem is, as you might guess from the title, the background for an item in the ListView
is not re-rendered whenever I select it. In my experience, this triggers the fact that I did not handle event fired when the selection is made. So I added
<ListView x:Name="myList" SelectionChanged="OnItemSelected">
<!-- Omitted code -->
</ListView>
Unfortunately, there is no place in the API telling me how to properly redraw the ListView
. The closest thing I could get is by this stupid code:
void MainPage::OnItemSelected(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
// This is to trigger OnItemTemplateChanged which have a side effect of redrawing the whole list view.
myList->ItemTemplate = myList->ItemTemplate;
}
Now, the new problem is that the list view flickers after every selection and the viewing state of the list is totally lost (e.g. the list automatically scrolls back to the beginning of the list).
So I want to ask for the proper way to force a ListView
(or any other UI element) to redraw itself.