Hello guys,
I'm struggling to change the Button visibility when the ListView is not empty. I have tried to create an ValueConvertor to return false when my list is empty and true when the list has 1 or more items. The thing is that after the ListView gets 1 or more elements the IsVisible property of the button is not binding anymore. At the start of the application the "Convert" method of the ValueConverter is executed and the button visibility is set to false.
This is my xaml code :
<ContentPage.BindingContext> <viewModels:OrdersPageViewModel/> </ContentPage.BindingContext> <StackLayout> <ListView x:Name="OdersViewList"> ... </ListView> <Button Text="Commit" Clicked="Button_CommitClicked" HorizontalOptions="Center" IsVisible="{Binding OrderListItems, Converter={StaticResource ListIsNull}}"/> </StackLayout>
This is my ViewModel:
public class OrdersPageViewModel : INotifyPropertyChanged { public ObservableCollection<OrderListItem> OrderListItems {get; set;} public event PropertyChangedEventHandler PropertyChanged; public OrdersPageViewModel() { OrderListItems = new ObservableCollection<OrderListItem>(); OrderListItems.CollectionChanged += OrderListItems_CollectionChanged; } void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } private void OrderListItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { OnPropertyChanged("OrderListItems"); } }
This is my ValueConverter:
public class ListIsNullConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { ObservableCollection<OrderListItem> list = (ObservableCollection<OrderListItem>)value; if (list.Count == 0) { return false; } return true; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }