I have a switch inside a Listview. I need to turn on all the switches when selecting an option from DisplayActionSheet
. How can I do this feature?
Xaml Code:
<ListView x:Name="UserList"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal"> <Label Text="{Binding fullname}" Font="11" TextColor="Black" HorizontalOptions="Start" VerticalOptions="Center"/> <Switch HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/> </StackLayout> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
public class Model : INotifyPropertyChanged { private string _fullname; public string fullname { get { return _fullname; } set { if (value != this._fullname) { this._fullname = value; NotifyPropertyChanged(); } } } private bool _toggled; public bool toggled { get { return _toggled; } set { if (value != this._toggled) { this._toggled = value; NotifyPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
List<Model> list = new List<Model>(); public Page3() { InitializeComponent(); list.Add(new Model { fullname = "1", toggled = true }); list.Add(new Model { fullname = "2", toggled = false }); list.Add(new Model { fullname = "3", toggled = false }); list.Add(new Model { fullname = "4", toggled = true }); list.Add(new Model { fullname = "5", toggled = false }); UserList.ItemsSource = list; }
private async void Button_Clicked(object sender, EventArgs e) { string option = await DisplayActionSheet("Title", null, null, new string[] { "Turn on", "Turn off" }); if (option == "Turn on") { foreach (Model model in list) { model.toggled = true; } } else if (option == "Turn off") { foreach (Model model in list) { model.toggled = false; } } }
In Addition , this the basic data binding , read the documentation https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-binding-basics ,https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm , next time you would implement by yourself instead asking here .
Answers
The items in your list view should be bound to items in an observable list. Then each switch in the view cell is bound to a property on the item. Your code simply iterates through everything in your observable list, setting that property to true.
@JohnH Didn't get you. Can you show some code?
This is pretty basic Xamarin Forms and MVVM.
In your example above, you already have a binding for the label Text in the viewcell view. You want another binding in the switch to a boolean property in the same item model.
Then in your command or action, iterate through all the items and set that property to true.
Model class
Populate the data , set it to ListView's itemsource.
Button click to change the data
In Addition , this the basic data binding , read the documentation https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-binding-basics ,https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm , next time you would implement by yourself instead asking here .