Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Binding Command for Button inside a ListView and Toggled Action from Switch inside ListView

$
0
0

Hi everyone,

I'm trying to add a command to a button inside a listview in a MVVM architecture, but i'm not being so succeed. Also i'm trying to attach an action to when the switch is toggled, but i have no idea how to do that (and i know that i can put an method inside the SET property in the MODEL, but i want to call an method from the view model). The data from the notification model (Time, Legend, Activated and AlarmStatus) is appearing on the screen and the button outside the listview is working as well... See bellow my code until now:

XAML:

<?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="AppTest.Views.NotifyManager">
  <ContentPage.Content>
    <StackLayout Padding="25">
      <Button Text="Add" Command="{Binding AddNotify} />
      <ListView ItemsSource="{Binding Notifications}" HasUnevenRows="True" SelectedItem="{Binding notifSelected}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <StackLayout>
                  <Label Text="{Binding Time}" FontSize="20"/>
                  <Label Text="{Binding Legend}" FontSize="15"/>
                  <Switch IsToggled="{Binding Activated}" />
                  <Label Text="{Binding AlarmStatus}" FontSize="15"/>
                  <Button Text="Remove" Command="{Binding ???}"/>
                </StackLayout>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

and the view model:

namespace AppTest.ViewModel
{
    class NotifyManagerViewModel
    {
        SaveNotification sn;
        public ObservableCollection<Notification> Notifications { get; set; }
        private NotificationDAO dao;
        public ICommand AddNotify { get; protected set; }
        public INavigation Navigation { get; set; }

        private Notification notifSelected;
        public Notification notifSelected
        {
            get
            {
                return notifSelected;
            }
            set
            {
                if(notifSelected != value)
                {
                    notifSelected = value;
                }
            }
        }

        public NotifyManagerViewModel(NotificationDAO notDao)
        {
            dao = notDao;
            Notifications = dao.Notifications;

            AddNotify = new Command(() => 
            {
                sn = new SaveNotification(dao);
                Navigation.PushAsync(sn);
            });
        }

    }
}

Thanks :)


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>