locked
ListView behaviour in UWP project RRS feed

  • Question

  • User392993 posted

    I've just started working with Xamarin Forms and I'm trying to wrap my head around a couple of things that are (not) happening relating to a bound ListView, hopefully someone can shed some light on this for me.

        private Item _SelectedItem;        
        public Item SelectedItem;
        {
           get => _SelectedItem;
           set
           {
              _SelectedItem; = value;
              NotifyPropertyChanged("SelectedItem");
            }
        }
    
        public ObservableCollection<Item> ItemsList { get; set; }
        public ICommand RemoveItemCommand { get; private set; }
    
    
        public MyViewModel()
        {
           RemoveItemCommand = new Command(RemoveItem, CanRemoveItem);
        }
    
        private void RemoveItem()
        {
           ItemsList.Remove(SelectedItem);
        }
    
        private bool CanRemoveItem()
        {            
           return SelectedItem != null;
           //return true;
        }
    

    And the Xaml:

    Wednesday, May 13, 2020 3:35 PM

Answers

  • User371688 posted

    You can add a bool variable in your ViewModel,and bind the bool variable to your the property IsEnabled of the REMOVE Button . I created a demo and it works well. You can refer to the following code:

     public class MyViewModel: INotifyPropertyChanged
        {
            private VeggieViewModel _SelectedItem;
            public VeggieViewModel SelectedItem
           {
           get => _SelectedItem;
           set
           {
              _SelectedItem = value;
    
              OnPropertyChanged("SelectedItem");
    
              CanRemove= SelectedItem != null; 
           }
          }
       // add a bool variable here
            private bool _canRemove;
            public bool CanRemove
            {
                get => _canRemove;
                set
                {
                    _canRemove = value;
    
                    OnPropertyChanged("CanRemove");
    
                }
            }
    
            public ICommand RemoveItemCommand { get; private set; }
    
            private ObservableCollection<VeggieViewModel> _veggies = new ObservableCollection<VeggieViewModel>();
    
            public ObservableCollection<VeggieViewModel> Veggies
            {
                get => _veggies;
                set
                {
                    _veggies = value;
                    OnPropertyChanged(nameof(Veggies));
                }
            }
    
            public MyViewModel()
            {
                Veggies = new ObservableCollection<VeggieViewModel>();
    
                Veggies.Add(new VeggieViewModel { Name = "Tomato", Type = "Fruit", Image = "tomato.png" ,NotificationColor= "#00FF00" });
                Veggies.Add(new VeggieViewModel { Name = "Romaine Lettuce", Type = "Vegetable", Image = "lettuce.png", NotificationColor = "#00FF00" });
                Veggies.Add(new VeggieViewModel { Name = "Zucchini", Type = "Vegetable", Image = "zucchini.png", NotificationColor = "#00FF00" });
    
                RemoveItemCommand = new Command(RemoveItem, CanRemoveItem);
            }
    
            private void RemoveItem()
            {
                Veggies.Remove(SelectedItem);
    
              // when we remove the SelectedItem, we need to reset the value of `CanRemove`
                CanRemove = false;
            }
    
            private bool CanRemoveItem()
            {
                return SelectedItem != null;
            }
    
    
            #region INotifyPropertyChanged
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
        }
    

    xaml code:

    <Button Text="REMOVE" Command="{Binding RemoveItemCommand}" IsEnabled="{Binding CanRemove}"/>
    

    The result is:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 14, 2020 9:25 AM
  • User371688 posted

    I have uploaded the demo in the attachment, you can check it here. Hope it can help you.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 14, 2020 9:39 AM

All replies

  • User392993 posted

    XAML:

       <Button Text="REMOVE" Command="{Binding RemovePraiseTrainingAidCommand}"/>
    
        <ListView ItemsSource="{Binding ItemsList}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemTemplate="{StaticResource ItemTemplate}" VerticalOptions="StartAndExpand"/>
    
    Wednesday, May 13, 2020 3:37 PM
  • User392993 posted

    oops, command should read

    <Button Text="Remove" Command="{Binding RemoveItemCommand}"/>

    Wednesday, May 13, 2020 3:39 PM
  • User371688 posted

    You can add a bool variable in your ViewModel,and bind the bool variable to your the property IsEnabled of the REMOVE Button . I created a demo and it works well. You can refer to the following code:

     public class MyViewModel: INotifyPropertyChanged
        {
            private VeggieViewModel _SelectedItem;
            public VeggieViewModel SelectedItem
           {
           get => _SelectedItem;
           set
           {
              _SelectedItem = value;
    
              OnPropertyChanged("SelectedItem");
    
              CanRemove= SelectedItem != null; 
           }
          }
       // add a bool variable here
            private bool _canRemove;
            public bool CanRemove
            {
                get => _canRemove;
                set
                {
                    _canRemove = value;
    
                    OnPropertyChanged("CanRemove");
    
                }
            }
    
            public ICommand RemoveItemCommand { get; private set; }
    
            private ObservableCollection<VeggieViewModel> _veggies = new ObservableCollection<VeggieViewModel>();
    
            public ObservableCollection<VeggieViewModel> Veggies
            {
                get => _veggies;
                set
                {
                    _veggies = value;
                    OnPropertyChanged(nameof(Veggies));
                }
            }
    
            public MyViewModel()
            {
                Veggies = new ObservableCollection<VeggieViewModel>();
    
                Veggies.Add(new VeggieViewModel { Name = "Tomato", Type = "Fruit", Image = "tomato.png" ,NotificationColor= "#00FF00" });
                Veggies.Add(new VeggieViewModel { Name = "Romaine Lettuce", Type = "Vegetable", Image = "lettuce.png", NotificationColor = "#00FF00" });
                Veggies.Add(new VeggieViewModel { Name = "Zucchini", Type = "Vegetable", Image = "zucchini.png", NotificationColor = "#00FF00" });
    
                RemoveItemCommand = new Command(RemoveItem, CanRemoveItem);
            }
    
            private void RemoveItem()
            {
                Veggies.Remove(SelectedItem);
    
              // when we remove the SelectedItem, we need to reset the value of `CanRemove`
                CanRemove = false;
            }
    
            private bool CanRemoveItem()
            {
                return SelectedItem != null;
            }
    
    
            #region INotifyPropertyChanged
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
        }
    

    xaml code:

    <Button Text="REMOVE" Command="{Binding RemoveItemCommand}" IsEnabled="{Binding CanRemove}"/>
    

    The result is:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 14, 2020 9:25 AM
  • User371688 posted

    I have uploaded the demo in the attachment, you can check it here. Hope it can help you.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 14, 2020 9:39 AM
  • User371688 posted

    Hi, @RcmpAdu, have you resolved your question?

    Tuesday, May 19, 2020 2:03 AM
  • User392993 posted

    Hi jezh sorry for the delay in responding, your demo was very helpful and I was able to resolve my issues from your explanation. Thank you very much for all the help!

    Tuesday, May 19, 2020 2:39 PM
  • User371688 posted

    It's my pleasure to help you. Have a nice day. :)

    Tuesday, May 19, 2020 9:48 PM