Here is one possible MVVM approach, which involve no code-behind.
ViewModels
Add a property* in your ListView
item viewmodel to store information that indicate whether the item is currently selected :
public class ItemViewModel : INotifyPropertyChanged
{
......
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if(_selectedItem != value)
{
_isSelected = value;
OnPropertyChanged();
}
}
}
}
Then in the page's viewmodel, have another property* for binding ListView
's SelectedItem
, in the setter of which you can setup IsSelected
value accordingly :
private ItemViewModel _selectedItem;
public ItemViewModel SelectedItem
{
get { return _selectedItem; }
set
{
if(_selectedItem != value)
{
//update previously selected item, if any :
if(_selectedItem != null) _selectedItem.IsSelected = false;
//update currently selected item :
value.IsSelected = true;
_selectedItem = value;
OnPropertyChanged();
}
}
}
View
Bind IsVisible
property to the ListView
item viewmodel :
<Label Text="Show only if selected" IsVisible="{Binding IsSelected}"/>
*) The property needs to be a public property, in a class that implements INotifyPropertyChanged
or inherits other class that implements it, and the property raises property-changed notification accordingly, as exemplified in the above snippets.
For reference: