Skip to content

Instantly share code, notes, and snippets.

@har07
Last active February 24, 2023 17:16
Show Gist options
  • Save har07/dd80c8316a08d48ea1f22e733339b7fc to your computer and use it in GitHub Desktop.
Save har07/dd80c8316a08d48ea1f22e733339b7fc to your computer and use it in GitHub Desktop.
Xamarin.Form : Binding item's IsVisible according to ListView selection
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace BasicListView
{
public class BaseViewModel : INotifyPropertyChanged
{
/*
* Reference: https://github.com/xamarin/xamarin-forms-book-preview-2/blob/master/Libraries/Xamarin.FormsBook.Toolkit/Xamarin.FormsBook.Toolkit/ViewModelBase.cs
*
*/
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
}
namespace BasicListView.ListViewSelectedItem
{
public class ItemViewModel : BaseViewModel
{
public string Data { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set { SetField(ref _isSelected, value); }
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BasicListView.ListViewSelectedItem.MainPage"
xmlns:local="clr-namespace:BasicListView.ListViewSelectedItem;assembly=BasicListView">
<ContentPage.BindingContext>
<local:MainViewModel/>
</ContentPage.BindingContext>
<ListView x:Name="listView" ItemsSource="{Binding Data}" SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Data}"/>
<Label Text="Show only if selected" IsVisible="{Binding IsSelected}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
using System.Collections.ObjectModel;
using System.Linq;
namespace BasicListView.ListViewSelectedItem
{
public class MainViewModel : BaseViewModel
{
private ObservableCollection<ItemViewModel> _data;
public ObservableCollection<ItemViewModel> Data
{
get { return _data; }
set { SetField(ref _data, value); }
}
private ItemViewModel _selectedItem;
public ItemViewModel SelectedItem
{
get { return _selectedItem; }
set
{
if(_selectedItem != value)
{
if(_selectedItem != null) _selectedItem.IsSelected = false;
value.IsSelected = true;
_selectedItem = value;
OnPropertyChanged();
}
}
}
public MainVM()
{
var query = Enumerable.Range(0, 10)
.Select(o => new ItemViewModel
{
Data = $"I am item no. {o}"
});
Data = new ObservableCollection<ItemViewModel>(query);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment